php call array from string

后端 未结 4 399
面向向阳花
面向向阳花 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:52

    I think regexp should do the trick better:

    $array['some']['string'] = 'test';
    $str = '[some][string]';
    if (preg_match('/\[(?\w+)\]\[(?\w+)\]/', $str, $keys))
    {
        if (isset($array[$keys['key1']][$keys['key2']]))
             echo $array[$keys['key1']][$keys['key2']]; // do what you need
    }
    

    But I would think twice before dealing with arrays your way :D

提交回复
热议问题