Abh*_*hvi 5 php arrays dynamic
我在php中访问数组时遇到了问题.
$path = "['a']['b']['c']";
$value = $array.$path;
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我有一个名为$ array的多维数组.
$path 是一个动态值,我将从数据库中获取.
现在我想使用$ path从$ array中检索值,但我无法.
$value = $array.$path
Run Code Online (Sandbox Code Playgroud)
回报我
Array['a']['b']['c']
Run Code Online (Sandbox Code Playgroud)
而不是价值.
我希望我已经正确地解释了我的问题.
Alm*_* Do 11
你有两个选择.首先(邪恶)如果要使用eval()函数 - 即将您的字符串解释为代码.
其次是解析你的道路.那将是:
//$path = "['a']['b']['c']";
preg_match_all("/\['(.*?)'\]/", $path, $rgMatches);
$rgResult = $array;
foreach($rgMatches[1] as $sPath)
{
$rgResult=$rgResult[$sPath];
}
Run Code Online (Sandbox Code Playgroud)