我有以下stdObject通捲曲獲得/ json_decode():如何在不引發PHP通知/警告的情況下循環訪問stdObject的數組?
stdClass Object
(
[response] => stdClass Object
(
[status] => OK
[num_elements] => 1030
[start_element] => 0
[results] => stdClass Object
(
[publisher] => stdClass Object
(
[num_elements] => 1030
[results] => Array
(
[0] => stdClass Object
(
[id] => 1234
[weight] => 4444
[name] => Pub 1
[member_id] => 1
[state] => active
[code] =>
)
[1] => stdClass Object
(
[id] => 1235
[weight] => 4444
[name] => Pub 2
[member_id] => 2
[state] => active
[code] =>
)
)
)
)
[dbg_info] => stdClass Object
(
[instance] => instance1.server.com
[slave_hit] => 1
[db] => db1.server.com
[reads] => 3
[read_limit] => 100
[read_limit_seconds] => 60
[writes] => 0
[write_limit] => 60
[write_limit_seconds] => 60
[awesomesauce_cache_used] =>
[count_cache_used] =>
[warnings] => Array
(
)
[time] => 70.440053939819
[start_microtime] => 1380833763.4083
[version] => 1.14
[slave_lag] => 0
[member_last_modified_age] => 2083072
)
)
)
我通過它循環,以獲得每一個結果的ID:
foreach ($result->response->results->publisher->results as $object) {
$publishers .= $object->id.",";
}
而雖然代碼工作正常,PHP上升以下事項/警告:
PHP的通知:試圖讓非對象的財產/var/www/vhosts/domain.net/script.php 1號線 PHP注意:試圖在/var/www/vhosts/domain.net/script.php中獲取非對象的屬性,第1行 PHP警告:在/ var/www/vhosts/domain中爲foreach()提供了無效參數.net/script.php上線1
任何想法?提前致謝!
你好@abstr,謝謝你的回答!你的解決方案看起來像一個很好的選擇,但不幸的是,json_decode在整個應用程序中使用的通用函數中調用。因此,任何更改都需要對應用程序的其餘部分進行大量修改。奇怪的是,這是拋出PHP通知的唯一循環,我想了解更多爲什麼會發生。最好的祝福! – Edward
@愛德華我更新了我的回答 – abstr
你說得對,問題是由於我使用的API的一個未記錄的「特性」造成的。感謝您的好評! – Edward