Re: How to use a variable variable in an array walk?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 





On 12/13/2011 5:43 PM, Nils Leideck wrote:
Anyone?    :-(

is my description too unclear?

On 11.12.2011, at 11:25, Nils Leideck wrote:

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!

Cheers, Nils

I'm short of time to conjure this in detail; but, on the surface it seems like nested foreach()s would do the trick.

foreach($myArray as $key1 => $userArray)
{
    foreach($userArray as $key2 => $designArray)
    {
        foreach($designArray as $key3 => $colorsArray)
        {
           foreach($colorsArray as $key4=>$itemsArray){
               //do stuff here. All keys are available

                }
            }
        }
    }
}





--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux