将字符串用于多维数组键

$lookup_table = array ("a" => "['foo']['bar']", "b" => "['foo']['man'][0]");

$foo = array ("a" => array ("bar" => "my value"));

var_dump ($foo['a']['bar']); //output: my value

What I want to do is put ['a']['bar'] as a string and basically make a little array that holds a key and the value or location in the array where the value would be.

$key = "['a']['bar']"; and then do $x = $foo[$key]; and have $x = "my value".

I realize I already put square brackets in the string and I'm doing it again above but I'm not sure how I would write it in the string.

$lookup_table = array ("a" => "['foo']['bar']", "b" => "['foo']['man'][0]");
$foo = array ("a" => array ("bar" => "my value"), "b" => array("man" => array("blah")));

echo getValue($lookup_table, $foo);

echo "
";



function getValue($lookup, $source)
{
    foreach ($lookup as $k => $v)
    {
        $v = str_replace("'", "", $v);
        $v = ltrim(rtrim($v, "]"), "[");

        $values = explode("][", $v);
        $data = $source[$k];

        for ($i = 1; $i < count($values); $i++) 
        { 
            $data = $data[$values[$i]];

            if($i == (count($values) - 1))
                echo $k . " = " . $data . "
";
        }
    }
}

Output:

a = my value
b = blah

I don't think you need to use the ' because you kind of declaring the key .. so the function can just use it as int and string the same.

So, basically what I did is: 1. Looping thought all the keys with the formatted arrays. 2. Skipping the first one because its the actual variable name 3. Looping until we reach the final value and then we display it.

Btw, do wanted to actually find the $foo as well ? if yes. let me know and I'll edit the code.