访问由PHP中的JSON解码产生的数组 [英] Accessing an array resulting from a JSON decoding in PHP
问题描述
我有这个数组,这是在PHP中解码JSON的结果(该数组的名称为$ data):
I have this array which is a result of decoding JSON in PHP (the array's name is $data):
Array ( [books] => Array ( [0] => Array ( [author] => author name [comment] => this is a great book ) [1] => Array ( [author] => another name [comment] => a comment ) ) )
我正在尝试按以下方式访问此数组:
I am trying to access this array as follows:
$data->books[0]->author
例如,使用
或$ data-> books以获得包括作者或评论的书籍的数组,但都不起作用,那么我在这里做错了什么?谢谢!
for example or $data->books to get an array of the books including author or comment but neither is working, what am I doing wrong here? Thanks!
推荐答案
在PHP中解码JSON对象时,它将表示为(关联)数组.起初这可能有点令人困惑,因为在JavaScript中解码JSON对象通常会返回JavaScriptObject表示形式.
When decoding a JSON object in PHP it will be represented as an (associative) array. This might be a little confusing at first since decoding a JSON object in JavaScript usually return a JavaScriptObject representation.
$data['books'][0]['author'];
这篇关于访问由PHP中的JSON解码产生的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!