Convert a String to Variable

后端 未结 9 2153
臣服心动
臣服心动 2020-12-01 05:43

I\'ve got a multidimensional associative array which includes an elements like

$data[\"status\"]
$data[\"response\"][\"url\"]
$data[\"entry\"][\"0\"][\"text\         


        
9条回答
  •  庸人自扰
    2020-12-01 06:32

    I was struggling with that as well, I had this :

    $user  =  array('a'=>'alber', 'b'=>'brad'...);
    
    $array_name = 'user';
    

    and I was wondering how to get into albert.

    at first I tried

    $value_for_a = $$array_name['a']; // this dosen't work 
    

    then

    eval('return $'.$array_name['a'].';'); // this dosen't work, maybe the hoster block eval which is very common
    

    then finally I tried the stupid thing:

    $array_temp=$$array_name;
    $value_for_a = $array_temp['a'];
    

    and this just worked Perfect! wisdom, do it simple do it stupid.

    I hope this answers your question

提交回复
热议问题