How to access object properties with names like integers?

后端 未结 7 941
故里飘歌
故里飘歌 2020-11-21 22:14

I am using json_decode() something like:

$myVar = json_decode($data)

Which gives me output like this:

[highlig         


        
7条回答
  •  梦如初夏
    2020-11-21 22:47

    A final alternative to Jon's comprehensive answer:

    Simply use json_decode() with the second parameter set to true.

    $array = json_decode($url, true);
    

    This then returns an associative array rather than an object so no need to convert after the fact.

    This may not be suitable to every application but it really helped me to easily reference a property of the oroginal object.

    Solution was found in this tutorial - http://nitschinger.at/Handling-JSON-like-a-boss-in-PHP/

    Regards

提交回复
热议问题