Php 检索json数组中的对象

Php 检索json数组中的对象,php,json,Php,Json,我正在尝试检索team1分数,但我似乎无法确定如何输出该分数。到目前为止,我已经在$obj是json输出的地方实现了这一点 $obj->recent JSON 您需要使用json\u decode将其放入数组中。它看起来像是最近的一个对象数组。所以,你会做一些类似的事情 $json = json_decode($obj->recent, true); $team1 = $json[0][0]['team 1']; //should return array $score = $te

我正在尝试检索team1分数,但我似乎无法确定如何输出该分数。到目前为止,我已经在$obj是json输出的地方实现了这一点

$obj->recent
JSON


您需要使用
json\u decode
将其放入数组中。它看起来像是最近的一个对象数组。所以,你会做一些类似的事情

$json = json_decode($obj->recent, true);
$team1 = $json[0][0]['team 1']; //should return array
$score = $team1['score']

编辑:感谢您的评论,在
json_decode
中缺少一个
true
作为第二个参数。您需要使用json_decode();此函数用于返回包含数组和对象的正确对象。现在需要检查什么是对象,什么是数组

$obj = json_decode($obj, true);
$obj->recent; //array 
$obj->recent[0]; //first element of array
$obj->recent[0][0]; //first element of second array
$obj->recent[0][0]->{'team 1'}; //access to object team 1
$obj->recent[0][0]->{'team 1'}->score; //access to property of object team 1
你可以发现有助于理解发生了什么

您还可以在上查看示例


如果在$obj上使用var_dump函数,它将显示什么是数组,什么是对象。

$obj=json_decode($json)
$obj->recent[0][0]->{'team 1'}->score
?如果不将true作为第二个参数传递给json_decode,则结果是一个对象,而不是一个数组,这将抛出错误。
$obj = json_decode($obj, true);
$obj->recent; //array 
$obj->recent[0]; //first element of array
$obj->recent[0][0]; //first element of second array
$obj->recent[0][0]->{'team 1'}; //access to object team 1
$obj->recent[0][0]->{'team 1'}->score; //access to property of object team 1