Dynamically "building/ targeting "arrays

With $$ PHP evaluates the string value of the inner $ as a variable name. One can even target a specific key by doing something like ${$ArrayName}[$key]. But what if you had a multidimensionally variable numeric OR Associative array. In another words it could be $X[key] or $ $X[key][key] or $X[key][key][key]. Does anyone know of a method to evaluate a string representing the name of such array?

I guess what I am asking is for something to fix this so it works:

$vari=“MyVar[‘x’][0][‘key’][1]”;
$vari=“MyVar[‘x’][‘key’]”;// other possible values
$vari=“MyVar[0][1]”;// other possible values
$vari=“MyVar”;// other possible values
$vari=“MyVar[‘someKey’]”;// other possible values
$check=$vari

As always any suggestions greatly appreciated

Hi Dresden,

You might want to see if the The RecursiveIteratorIterator class to do something like this that can return the $values and/or $keys from any unknown level array.

 
<?php
$base1_depth = array('base1-1' => 'r1', 'base1-2' => 'r2');
$base2_depth = array('base2-1' => 'b2_1', 'base2-2' => 'b2_2');
$base3_depth = array('base3-1' => 'b3_1', 'base3-2' => 'b3_2');
$depths = array('depth1' => $base3_depth, 'depth2' => $base2_depth, 'depth3' => $base1_depth);

$array_obj = new RecursiveIteratorIterator(new RecursiveArrayIterator($depths));
foreach($array_obj as $key => $value) {
   echo $key . ': ' . $value . '<br />';
}
/*Outputs*/
base3-1: b3_1
base3-2: b3_2
base2-1: b2_1
base2-2: b2_2
base1-1: r1
base1-2: r2
?>
 

You can make use of the internal foreach logic as you like to evaluate string values that you return.

Regards,
Steve