PHP“Cannot use object of type stdClass as array”
生活随笔
收集整理的這篇文章主要介紹了
PHP“Cannot use object of type stdClass as array”
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
php再調(diào)用json_decode從字符串對象生成json對象時,如果使用[]操作符取數(shù)據(jù),會得到下面的錯誤
錯誤:
Cannot use object of type stdClass as array
產(chǎn)生原因:
+展開 -PHP $res?=?json_decode($res);
????$res['key'];?//把?json_decode()?后的對象當(dāng)作數(shù)組使用。
解決方法(2種):
1、使用 json_decode($d, true)。就是使json_decode 的第二個變量設(shè)置為 true。
2、json_decode($res) 返回的是一個對象, 不可以使用 $res['key'] 進(jìn)行訪問, 換成 $res->key 就可以了。
參考手冊:json_decode
Return Values:Returns an object or if the optional assoc parameter is TRUE, an associative array is instead returned.
錯誤:
Cannot use object of type stdClass as array
產(chǎn)生原因:
+展開 -PHP $res?=?json_decode($res);
????$res['key'];?//把?json_decode()?后的對象當(dāng)作數(shù)組使用。
解決方法(2種):
1、使用 json_decode($d, true)。就是使json_decode 的第二個變量設(shè)置為 true。
2、json_decode($res) 返回的是一個對象, 不可以使用 $res['key'] 進(jìn)行訪問, 換成 $res->key 就可以了。
參考手冊:json_decode
Return Values:Returns an object or if the optional assoc parameter is TRUE, an associative array is instead returned.
總結(jié)
以上是生活随笔為你收集整理的PHP“Cannot use object of type stdClass as array”的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 初次体验hiphop-php
- 下一篇: 黄山风景区三天怎么玩