$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.
我想要做的是将['a'] ['bar']作为一个字符串,并基本上创建一个小数组,其中包含一个键以及值所在的数组中的值或位置。
$key = "['a']['bar']";
and then do $x = $foo[$key];
and have $x = "my value".
$ key =“['a'] ['bar']”;然后做$ x = $ foo [$ key];并且$ x =“我的价值”。
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.
我意识到我已经在字符串中放了方括号,我在上面再做一遍,但我不确定如何将它写在字符串中。
1 个解决方案
#1
$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 "\n";
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 . "\n";
}
}
}
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.
我不认为你需要使用'因为你要声明键...所以函数可以只使用它作为int和字符串相同。
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.
所以,基本上我所做的是:1。循环使用格式化数组的所有键。 2.跳过第一个因为它的实际变量名称3.循环直到我们到达最终值然后我们显示它。
Btw, do wanted to actually find the $foo
as well ? if yes. let me know and I'll edit the code.
顺便说一下,还想真正找到$ foo吗?如是。让我知道,我将编辑代码。
#1
$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 "\n";
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 . "\n";
}
}
}
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.
我不认为你需要使用'因为你要声明键...所以函数可以只使用它作为int和字符串相同。
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.
所以,基本上我所做的是:1。循环使用格式化数组的所有键。 2.跳过第一个因为它的实际变量名称3.循环直到我们到达最终值然后我们显示它。
Btw, do wanted to actually find the $foo
as well ? if yes. let me know and I'll edit the code.
顺便说一下,还想真正找到$ foo吗?如是。让我知道,我将编辑代码。