php call array from string

后端 未结 4 396
面向向阳花
面向向阳花 2021-01-23 02:57

I have a string that contains elements from array.

$str = \'[some][string]\';
$array = array();

How can I get the value of $array[\'some\

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-23 03:48

    This will work for any number of keys:

    $keys = explode('][', substr($str, 1, -1));
    $value = $array;
    foreach($keys as $key)
        $value = $value[$key];
    
    echo $value
    

提交回复
热议问题