note 57920 deleted from function.explode by bjori
| From: | bjori@php.net | Date: | Mon, 25 Sep 2006 08:07:46 +0000 |
| Subject: | note 57920 deleted from function.explode by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-117575@lists.php.net to get a copy of this message | ||
Note Submitter: britz_pm at hotmail dot com
----
PLEASE NOTE I HAD TO BREAK SOME LINES CAUSE OF WORDWRAP() WAS NOT HAPPY :(
Well i thought of making some versions of explode/implode
functions with can do any depth of multi-dimensional arrays
with or without keeping the keys
make/change the defaults as you need them
as for error checking i did not add any because would probably
make it take longer to run add em if you please
Code:
<?php
function mul_dim_implode($curarray,$startglue,$endglue,
$withkeys=false,$startkeyglue=null,$endkeyglue=null,$level=0) {
foreach($curarray as $curkey => $curvalue) {
if (is_array($curvalue)) {
$curvalue = mul_dim_implode($curvalue,$startglue,$endglue,
$withkeys,$startkeyglue,$endkeyglue,$level+1);
}
if (!isset($retu)) {
if ($withkeys) {
$retu = $curkey.$startkeyglue.$level.$endkeyglue.$curvalue;
} else {
$retu = $curvalue;
}
} else {
if ($withkeys) {
$retu .= $startglue.$level.$endglue.$curkey.
$startkeyglue.$level.$endkeyglue.$curvalue;
} else {
$retu .= $startglue.$level.$endglue.$curvalue;
}
}
}
return $retu;
}
function mul_dim_explode($curstring,$startglue,$endglue,
$withkeys=false,$startkeyglue=null,$endkeyglue=null,$level=0) {
if (strstr($curstring,$startglue.$level.$endglue)) {
$eacharay = explode($startglue.$level.$endglue,$curstring);
foreach ($eacharay as $curstr) {
if ($withkeys) {
$temp = explode($startkeyglue.$level.$endkeyglue,$curstr);
$retu[$temp[0]] = mul_dim_explode($temp[1],$startglue,$endglue,
$withkeys,$startkeyglue,$endkeyglue,$level+1);
} else {
$retu[] = mul_dim_explode($curstr,$startglue,$endglue,
$withkeys,$startkeyglue,$endkeyglue,$level+1);
}
}
} else {
return $curstring;
}
return $retu;
}
?>
Example:
<?php
$array = array('a' => 'aa', 'b' => 'bb',
'c' => array('d' => 'dd', 'e'
=> 'ee',
'f' => array('g' => 'gg', 'h' =>
'hh')));
$plode = mul_dim_implode($array,"-[","]-");
$keyplode =
mul_dim_implode($array,"-[","]-",true,"={","}=");
echo($plode);
echo("\n<br/>\n");
echo($keyplode);
$unplode = mul_dim_explode($plode,"-[","]-");
$keyunplode =
mul_dim_explode($keyplode,"-[","]-",true,"={","}=");
echo("\n<pre>\n");
echo("Before:\n");
print_r($array);
echo("After:\n");
print_r($unplode);
print_r($keyunplode);
echo("</pre>");
?>
Output:
aa-[0]-bb-[0]-dd-[1]-ee-[1]-gg-[2]-hh
a={0}=aa-[0]-b={0}=bb-[0]-c={0}=d={1}=dd
-[1]-e={1}=ee-[1]-f={1}=g={2}=gg-[2]-h={2}=hh
Before:
Array
(
[a] => aa
[b] => bb
[c] => Array
(
[d] => dd
[e] => ee
[f] => Array
(
[g] => gg
[h] => hh
)
)
)
After:
Array
(
[0] => aa
[1] => bb
[2] => Array
(
[0] => dd
[1] => ee
[2] => Array
(
[0] => gg
[1] => hh
)
)
)
Array
(
[a] => aa
[b] => bb
[c] => Array
(
[d] => dd
[e] => ee
[f] => Array
(
[g] => gg
[h] => hh
)
)
)
Enjoy. ;p