note 89095 deleted from function.join by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 12:18:56 +0000 |
| Subject: | note 89095 deleted from function.join by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-197176@lists.php.net to get a copy of this message | ||
Note Submitter: datacompboy at call2ru dot com
----
Very useful function i have using: joinr (join-recursive).
It allows you easy join of multi-level arrays.
For example, if you have
$intervals = array(array(5, 9), array(11, 15), array(22, 24))
and want to have comma-separated list of dash-separated intervals, just do
print joinr(array(",","-"), $intervals);
Here is function code:
<?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);
}
?>