如何解析数组字符串以便获取变量的值
示例字符串
$str = 'prices[holiday:waterpark][person:1_person]';
样本变量
$prices['holiday:waterpark']['person:1_person'] = 100;
我尝试过在 php 中使用变量.variable 方式,如下所示
$prices['holiday:waterpark']['person:1_person'] = 100;
$str = "prices\['holiday:waterpark'\]\['person:1_person'\]";
$str = str_replace('\\', '', $str);
echo $$str;
但这不起作用,我收到错误
"Undefined variable: $prices['holiday:waterpark']['person:1_person']"
试试这个
<?
$prices['holiday:waterpark']['person:1_person'] = 100;
$str = "prices\['holiday:waterpark'\]\['person:1_person'\]";
$str = stripslashes($str);
// be careful with passing $str from untrusted source
// make necessary variable filtering before!
echo eval('return $' . $str . ';');