note 89095 added to function.join
| From: | datacompboy at call2ru dot com | Date: | Sat, 21 Feb 2009 17:43:02 +0000 |
| Subject: | note 89095 added to function.join | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-150920@lists.php.net to get a copy of this message | ||
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);
}
?>
----
Server IP: 78.41.104.32
Probable Submitter: 80.64.81.163
----
Manual Page -- http://www.php.net/manual/en/function.join.php
Edit -- https://master.php.net/note/edit/89095
Del: integrated -- https://master.php.net/note/delete/89095/integrated
Del: useless -- https://master.php.net/note/delete/89095/useless
Del: bad code -- https://master.php.net/note/delete/89095/bad+code
Del: spam -- https://master.php.net/note/delete/89095/spam
Del: non-english -- https://master.php.net/note/delete/89095/non-english
Del: in docs -- https://master.php.net/note/delete/89095/in+docs
Del: other reasons-- https://master.php.net/note/delete/89095
Reject -- https://master.php.net/note/reject/89095
Search -- https://master.php.net/manage/user-notes.php