Dynamic access to a PHP array

后端 未结 4 427
逝去的感伤
逝去的感伤 2021-01-13 05:27

I tried to access with $this->$arrDataName[$key] on the element with the key $key from the array $this->$arrDataName. But PHP in

4条回答
  •  不要未来只要你来
    2021-01-13 06:15

    Your syntax is correct:

    $this->{$varName}[$key]
    

    You can also use an extra variable for this:

    $myTempArr = $this->$arrDataName;
    
    $myTempArr[ $key ];
    

    IMHO, readability is better that way...

提交回复
热议问题