php 致命错误:无法使用 stdClass 类型的对象作为数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17518095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fatal error: Cannot use object of type stdClass as array
提问by Nick
I know this is probably something simple and I am beginning to understand how I need to go about this, I realize that this has something to do with OO however as i am new to OO and json im having a bit of a problem with this http://api.discogs.com/database/search?q=d&page=1&per_page=5it returns 5 results with the query d
in json format so i parse it with the php function and then attempt to lets say show the thubmnail for the first results this is my parse code:
我知道这可能很简单,我开始了解我需要如何去做,我意识到这与 OO 有关,但是因为我是 OO 和 json 的新手,所以我对这个http有点问题://api.discogs.com/database/search?q=d&page=1&per_page=5它d
以json格式返回5个查询结果,所以我用php函数解析它,然后尝试让我们说显示第一个缩略图结果这是我的解析代码:
$returnData = file_get_contents($queryURL);
$discogsJSON = json_decode($returnData);
where $queryURL
= the provided url. if i print $discogsJSON
i get this:
where $queryURL
= 提供的网址。如果我打印$discogsJSON
我得到这个:
<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>
which is exactly correct and i can see all the data i want to, however when i try to access the (lets say thumbnail of the first) like so:
这是完全正确的,我可以看到我想要的所有数据,但是当我尝试访问(可以说第一个缩略图)时,如下所示:
echo $discogsJSON['results']['0']['thumb'];
i am left with: Fatal error: Cannot use object of type stdClass as array
我只剩下: Fatal error: Cannot use object of type stdClass as array
I suppose from here there is enough information to go off of to construct the correct echo statement, but can anyone explain to me the OO theory behind the correct way to get the data out?
我想从这里开始有足够的信息来构建正确的 echo 语句,但是谁能向我解释正确获取数据的方法背后的 OO 理论?
回答by apartridge
$discogsJSON
is not an array, but an stdClass
object. results is a property on this object.
$discogsJSON
不是一个数组,而是一个stdClass
对象。结果是此对象的一个属性。
This should do what you are looking for.
这应该做你正在寻找的。
echo $discogsJSON->results['0']->thumb;
However, it may be more convinient to add a true
flag to json_decode
to make sure that the returned value is an associative array.
但是,添加一个true
标志以json_decode
确保返回值是关联数组可能更方便。
$discogsJSON = json_decode($returnData, true);
回答by Robot Boy
You need to make the array associative by adding trueas a second parameter of json_decode
您需要通过添加true作为json_decode的第二个参数 来使数组关联
$discogsJSON = json_decode($returnData, true);
$discogsJSON = json_decode($returnData, true);