我通过cURL / json_decode()获得了以下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注意:尝试在第1行的/var/www/vhosts/domain.net/script.php中获取非对象的属性 PHP注意:尝试在第1行的/var/www/vhosts/domain.net/script.php中获取非对象的属性 PHP警告:在第1行的/var/www/vhosts/domain.net/script.php中为foreach()提供的参数无效
有什么想法吗?提前谢谢!
答案 0 :(得分:0)
您是否可以将json_decode()中的第二个参数$assoc
设置为true
?像这样:
$result = json_decode($json_from_curl, true);
因此,返回$ result会将所有stdClass对象转换为关联数组。
编辑:
如果你得到的$result
确实是一个关联数组,那么我们应该这样对待它。请尝试将此foreach
替换为:
foreach ($result['response']['results']['publisher']['results'] as $arr) {
$publishers .= $arr['id'] . ",";
}
EDIT2: 根据我自己的代码测试,一切都应该正常工作。不应出现通知/警告。也许你问题中未显示的其他一些代码会导致它。