Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 致命错误:无法将stdClass类型的对象用作数组_Php_Arrays_Oop - Fatal编程技术网

Php 致命错误:无法将stdClass类型的对象用作数组

Php 致命错误:无法将stdClass类型的对象用作数组,php,arrays,oop,Php,Arrays,Oop,我知道这可能很简单,我开始明白我需要怎么做,我意识到这与OO有关,但由于我是OO和json新手,我对此有点问题,它返回5个json格式的查询结果d,因此我使用php函数对其进行解析,然后尝试对第一个结果显示图钉这是我的解析代码: $returnData = file_get_contents($queryURL); $discogsJSON = json_decode($returnData); <pre>stdClass Object ( [pagination] => s

我知道这可能很简单,我开始明白我需要怎么做,我意识到这与OO有关,但由于我是OO和json新手,我对此有点问题,它返回5个json格式的查询结果
d
,因此我使用php函数对其进行解析,然后尝试对第一个结果显示图钉这是我的解析代码:

$returnData = file_get_contents($queryURL);
$discogsJSON = json_decode($returnData);
<pre>stdClass Object
(
[pagination] => stdClass Object
    (
        [per_page] => 5
        [items] => 179465
        [page] => 1
        [urls] => stdClass Object
            (
                [last] => http://api.discogs.com/database/search?q=f&per_page=5&page=35893
                [next] => http://api.discogs.com/database/search?q=f&per_page=5&page=2
            )

        [pages] => 35893
    )

[results] => Array
    (
        [0] => stdClass Object
            (
                [style] => Array
                    (
                        [0] => Free Jazz
                    )

                [thumb] => http://api.discogs.com/image/R-90-1672955-1305426452.jpeg
                [format] => Array
                    (
                        [0] => Vinyl
                        [1] => LP
                        [2] => Album
                    )

                [country] => Switzerland
                [title] => Steve Lacy - Clinkers
                [uri] => /Steve-Lacy-Clinkers/master/54331
                [label] => Array
                    (
                        [0] => Hat Hut Records
                    )

                [catno] => F
                [year] => 1978
                [genre] => Array
                    (
                        [0] => Jazz
                    )

                [resource_url] => http://api.discogs.com/masters/54331
                [type] => master
                [id] => 54331
            )

        [1] => stdClass Object
            (
                [style] => Array
                    (
                        [0] => Free Jazz
                    )

                [thumb] => http://api.discogs.com/image/R-90-1672955-1305426452.jpeg
                [format] => Array
                    (
                        [0] => Vinyl
                        [1] => LP
                        [2] => Album
                    )

                [country] => Switzerland
                [title] => Steve Lacy - Clinkers
                [uri] => /Steve-Lacy-Clinkers/release/1672955
                [label] => Array
                    (
                        [0] => Hat Hut Records
                    )

                [catno] => F
                [year] => 1978
                [genre] => Array
                    (
                        [0] => Jazz
                    )

                [resource_url] => http://api.discogs.com/releases/1672955
                [type] => release
                [id] => 1672955
            )

        [2] => stdClass Object
            (
                [style] => Array
                    (
                        [0] => Alternative Rock
                        [1] => Goth Rock
                        [2] => Heavy Metal
                    )

                [thumb] => http://api.discogs.com/image/R-90-2785283-1300918365.jpeg
                [format] => Array
                    (
                        [0] => CD
                        [1] => Compilation
                        [2] => Unofficial Release
                    )

                [country] => Russia
                [title] => HIM (2) - Overload Hits
                [uri] => /HIM-Overload-Hits/release/2785283
                [label] => Array
                    (
                        [0] => FullHouse Records
                    )

                [catno] => F?????
                [year] => 2003
                [genre] => Array
                    (
                        [0] => Rock
                    )

                [resource_url] => http://api.discogs.com/releases/2785283
                [type] => release
                [id] => 2785283
            )

        [3] => stdClass Object
            (
                [style] => Array
                    (
                        [0] => Drone
                        [1] => Ambient
                    )

                [thumb] => http://api.discogs.com/image/R-90-3108621-1316178067.jpeg
                [format] => Array
                    (
                        [0] => CDr
                        [1] => Album
                        [2] => Limited Edition
                    )

                [country] => Russia
                [title] => Solar Ocean - Twillight October
                [uri] => /Solar-Ocean-Twillight-October/release/3108621
                [label] => Array
                    (
                        [0] => Necrophone
                    )

                [catno] => F
                [year] => 2010
                [genre] => Array
                    (
                        [0] => Electronic
                    )

                [resource_url] => http://api.discogs.com/releases/3108621
                [type] => release
                [id] => 3108621
            )

        [4] => stdClass Object
            (
                [style] => Array
                    (
                        [0] => Techno
                        [1] => Electro
                        [2] => Tech House
                        [3] => Minimal
                    )

                [thumb] => http://api.discogs.com/image/R-90-699054-1161976299.jpeg
                [format] => Array
                    (
                        [0] => Vinyl
                        [1] => 12"
                        [2] => Box Set
                        [3] => Compilation
                        [4] => Limited Edition
                    )

                [country] => Germany
                [title] => Various - Cocoon Compilation F
                [uri] => /Various-Cocoon-Compilation-F/master/33655
                [label] => Array
                    (
                        [0] => Cocoon Recordings
                        [1] => Cocoon Compilation
                    )

                [catno] => COR LP 011
                [year] => 2006
                [genre] => Array
                    (
                        [0] => Electronic
                    )

                [resource_url] => http://api.discogs.com/masters/33655
                [type] => master
                [id] => 33655
            )

    )

)
</pre>
其中
$queryURL
=提供的url。如果我打印
$discogsJSON
,我会得到以下结果:

留给我的是:
致命错误:无法将stdClass类型的对象用作数组


我想从这里可以得到足够的信息来构造正确的echo语句,但是有人能给我解释一下正确的数据获取方法背后的OO理论吗?

$discogsJSON
不是数组,而是一个
stdClass
对象。结果是此对象的属性

这应该符合你的要求

$discogsJSON = json_decode($returnData, true);
但是,将
true
标志添加到
json_decode
以确保返回值是关联数组可能更方便


您需要通过添加true作为json\u decode


$discogsJSON=json\u decode($returnData,true)

先生,您是我本周在互联网上最喜欢的人,谢谢!如果你满意这个答案,请接受。
echo $discogsJSON->results['0']->thumb;
$discogsJSON = json_decode($returnData, true);