访问由PHP中的JSON解码产生的数组 [英] Accessing an array resulting from a JSON decoding in PHP

查看:110
本文介绍了访问由PHP中的JSON解码产生的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个数组,这是在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆