Re: creating of multi dimensional arrays with unknown depth
| From: | Richard Lynch | Date: | Tue, 12 Jul 2005 05:55:44 +0000 |
| Subject: | Re: creating of multi dimensional arrays with unknown depth | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-218507@lists.php.net to get a copy of this message | ||
On Mon, July 11, 2005 10:56 am, Ben-Nes Yonatan said:
> Hi all!
>
> I got a string which include information for categories that I need to
> build, the string is built like this:
> $string = "val1~~val2~~val3~~val4~~val5"
>
> Now, I want to create from this string a multi dimensional array and not
> one dimension array as explode('~~', $string) will give me.
>
> The desired result from this string is:
>
> $array['val1'] = 'whatever';
> $array['val1']['val2'] = 'whatever';
> $array['val1']['val2']['val3'] = 'whatever';
> $array['val1']['val2']['val3']['val4'] =
> 'whatever';
>
> $array['val1']['val2']['val3']['val4']['val'5] =
> 'whatever';
That's not a valid array construct...
You can't have $array['val1'] be a string ('whatever') and also an array
(['val2'])..
Or is that 5 different examples?
Untested Code:
<?php
$string = "val1~~val2~~val3~~val4~~val5";
$keys = explode("~~", $string);
$keys = array_reverse($keys);
$array = 'whatever';
while (list(, $key) = each($keys)){
$array[$key] = $array;
}
var_dump($array);
?>
--
Like Music?
http://l-i-e.com/artists.htm