简体   繁体   English

访问JSON对象中的数组时出错

[英]Error when accessing array in JSON object

I've been looking all over for the solution to this problem, but everything I try always returns the same error. 我一直在寻找解决此问题的方法,但是我尝试的所有操作总是返回相同的错误。

Fatal error: Cannot use object of type stdClass as array 致命错误:不能将stdClass类型的对象用作数组

This is the JSON resource I'm trying to access with PHP cURL: https://www.easports.com/iframe/fifa16proclubs/api/platforms/PS4/clubs/36881/members 这是我正在尝试使用PHP cURL访问的JSON资源: https : //www.easports.com/iframe/fifa16proclubs/api/platforms/PS4/clubs/36881/members

The issue I'm having is accessing the raw array. 我遇到的问题是访问原始数组。 I've had success using the following code with objects, but I always have trouble when there is an array involved. 我已经成功将以下代码与对象一起使用,但是当涉及到数组时,我总是会遇到麻烦。

$phpObj = json_decode($json);

if (!is_null($phpObj->raw))
{   
    $object = $phpObj->raw;

    foreach ($object as $obj)
    {
       echo "<tr id='clubs-search-result'><td class='club-search-name'>" . $obj->name . "</td><td class='blaze-id hidden'>" . $obj->blazeId . "</td><td><button type='button' class='form-button players-search-button red-bg'>Select</button></td></tr>";
    }
}

I know that this is not the correct way to access an array, which is what I need help with. 我知道这不是访问数组的正确方法,这是我需要帮助的。 I want to be able to access the raw array and then iterate through all the objects inside the array. 我希望能够访问原始数组,然后遍历数组内的所有对象。 I've looked at many code examples but nothing seems to be working for my scenario. 我看过许多代码示例,但似乎没有任何适用于我的方案。

The above code works with this resource: http://www.easports.com/iframe/fifa16proclubs/api/platforms/PS4/clubsComplete/United 上面的代码与此资源一起使用: http : //www.easports.com/iframe/fifa16proclubs/api/platforms/PS4/clubsComplete/United

Only because I'm accessing nested objects and there are no arrays [] involved. 仅因为我正在访问嵌套对象,并且不涉及数组[]。

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks! 谢谢!

It appears the foreach loop needs to run on the first (and only) array in $phpObj->raw . 似乎foreach循环需要在$phpObj->raw的第一个(也是唯一一个)数组上运行。

You need to change the foreach to: 您需要将foreach更改为:

foreach ($object[0] as $obj) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM