How to use a variable variable in an array walk?
| From: | Nils Leideck | Date: | Sun, 11 Dec 2011 16:25:04 +0000 |
| Subject: | How to use a variable variable in an array walk? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-315978@lists.php.net to get a copy of this message | ||
Dear list,
this is my first post to the PHP general list.
I have an issue with a variable variable (http://php.net/manual/en/language.variables.variable.php)
My use case:
I have an array called $myArray. The structure is as following:
array(1) {
["user_interface"]=>
array(1) {
["design"]=>
array(1) {
[“my_colors"]=>
array(5) {
["item_number_one"]=>
string(6) "red"
["item_number_two"]=>
string(40)
'["user_interface"]["design"]["my_colors"]["item_number_one"]'
}
}
}
}
As you can see, the item_number_one has no direct color value assigned but the structure of the path
to item_number_one in the $myArray variable. I tried with array_wal_resursive. During this step (the
array building is completed) I want to find these values (I use a static value in my example, in the
real code I will use regular expressions) and assign the value of the virtually related item to the
considered item.
So in my example above, I want to have the following values after the process is done:
$myArray[user_interface][design][my_colors][item_mumber_one] = red; // this is item number 1
$myArray[user_interface][design][my_colors][item_mumber_two] = red; // this should be item number 2
The second issue here is, how do I evaluate at which point the process is exactly, because the value
and the key that is transferred to the function by array_walk_recursive has only the value itself
but not array path to the current item.
Any idea how get this done? Or am I too complicated maybe?
I tried several combinations of ${$var}, $myArray{$var}, {$myArray}{$var} ... and many more.
Any help is much much appreciated!
Merry X-mas greeting from Indianapolis
Cheers, Nils
--
http://webint.cryptonode.de / a Fractal project