Access multidimensional array by string with delimiter

前端 未结 1 1459
北海茫月
北海茫月 2021-01-20 17:13

suppose i have a multidimensional array like something like this:

 array(\"test2\" => array(\"test3\" => 1)), .         


        
相关标签:
1条回答
  • 2021-01-20 17:47

    You could use

    function get_multi($arr, $str) {
        foreach (explode('.', $str) as $key) {
            if (!array_key_exists($key, $arr)) {
                return NULL; 
            }
            $arr = $arr[$key];
        }
    
        return $arr;
    }
    
    0 讨论(0)
提交回复
热议问题