note 105454 added to function.implode
| From: | gu_ludo at yahoo dot com dot br | Date: | Fri, 19 Aug 2011 11:32:05 +0000 |
| Subject: | note 105454 added to function.implode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-182584@lists.php.net to get a copy of this message | ||
Hello there.
First, forgive me for my English if I write anything wrong. I'm brazilian.
I've made a recursive implode for multidimensional arrays. The $glue may be either an array or
a string. If array, each element will be used as glue for each level of $pieces. If count($glue) is
less than number of levels of $pieces, the last glue will be used for the remainder levels.
<?php
function implode_r($glue, $pieces){
$return = "";
if(!is_array($glue)){
$glue = array($glue);
}
$thisLevelGlue = array_shift($glue);
if(!count($glue)) $glue = array($thisLevelGlue);
if(!is_array($pieces)){
return (string) $pieces;
}
foreach($pieces as $sub){
$return .= implode_r($glue, $sub) . $thisLevelGlue;
}
if(count($pieces)) $return = substr($return, 0, strlen($return) -strlen($thisLevelGlue));
return $return;
}
//Example
$arr = array(
array(
'SecondLevelFirstElement',
'SecondLevelSecondElement'
),
'FirstLevelFirstElement',
'FirstLevelSecondElement'
);
echo implode_r(array("-|-", "_"));
?>
The output string will be:
"SecondLevelFirstElement_SecondLevelSecondElement-|-FirstLevelFirstElement-|-FirstLevelSecondElement"
----
Server IP: 69.147.83.197
Probable Submitter: 187.107.149.176
----
Manual Page -- http://www.php.net/manual/en/function.implode.php
Edit -- https://master.php.net/note/edit/105454
Del: integrated -- https://master.php.net/note/delete/105454/integrated
Del: useless -- https://master.php.net/note/delete/105454/useless
Del: bad code -- https://master.php.net/note/delete/105454/bad+code
Del: spam -- https://master.php.net/note/delete/105454/spam
Del: non-english -- https://master.php.net/note/delete/105454/non-english
Del: in docs -- https://master.php.net/note/delete/105454/in+docs
Del: other reasons-- https://master.php.net/note/delete/105454
Reject -- https://master.php.net/note/reject/105454
Search -- https://master.php.net/manage/user-notes.php