create multidimensional array with n depth
| From: | Gunter Sammet | Date: | Wed, 09 Aug 2006 17:14:17 +0000 |
| Subject: | create multidimensional array with n depth | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-240390@lists.php.net to get a copy of this message | ||
Hi all:
I've been trying to create a multidimensional array with n depth in php.
Tried to use a recursive function passing array references by pointers but
seems like it's not possible or I still don't understand this subject
enough. Finally managed to get it going using the eval function. The code
below converts a seperated string into a multi dimensional array with n
depth:
e.g. $array['1_20-2_16-7_14'] = 12 will become
$eval_array[1][20][2][16][7][14] = 12
foreach(array_keys($this->quantity_array) AS $key){
if($this->quantity_array[$key] > 0){
$combinations = explode('-', $key);
$eval_string = '$eval_array';
foreach(array_keys($combinations) AS $key2){
$option_key_value = explode('_', $combinations[$key2]);
$eval_string .=
'['.$option_key_value[0].']['.$option_key_value[1].']';
}
$eval_string .= ' = '.$this->quantity_array[$key].';';
eval($eval_string);
}
}
Using eval() for it seems a bit expensive to me. So I've been wondering if
there is an easier way?
TIA
Gunter