There was a recursive join function over at the join documentation on php.net. I wrote a version that was fully recursive on the delimiters, as well as more concise and understandable (IMO).
// original function <?php function joinr($join, $value, $lvl=0) { if (!is_array($join)) return joinr(array($join), $value, $lvl); $res = array(); if (is_array($value)&&sizeof($value)&&is_array(current($value))) { // Is value are array of sub-arrays? foreach($value as $val) $res[] = joinr($join, $val, $lvl+1); } elseif(is_array($value)) { $res = $value; } else $res[] = $value; return join(isset($join[$lvl])?$join[$lvl]:"", $res); }
// mine <?php function joinr($delim, $value) { $cpy = array (); foreach ( $value as $val ) { if ( is_array ( $delim )) $d = count($delim) > 1 ? array_slice($delim, 1) : $delim[0]; else $d = $delim; $cpy[] = is_array($val) ? joinr ( $d, $val) : $val; } return join (is_array($delim) ? $delim[0] : $delim, $cpy); } $intervals = array(array(5, array(3,4,5)), array(11, 15), array(22, 24)); echo joinr(array(",", "-", "!"), $intervals); // 5-3!4!5,11-15,22-24 ?>