0

我通过 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 注意:试图在 /var/www/vhosts/domain 中获取非对象的属性。第 1 行的 net/script.php PHP 警告:在第 1 行的 /var/www/vhosts/domain.net/script.php 中为 foreach() 提供的参数无效

有任何想法吗?提前致谢!

4

1 回答 1

0

您是否可以将$assocjson_decode() 中的第二个参数设置为true?像这样:

$result = json_decode($json_from_curl, true);

因此,返回 $result 并将所有 stdClass 对象转换为关联数组。

编辑:如果$result你得到的确实是一个关联数组,那么我们应该这样对待它。尝试用foreach这个替换你的:

foreach ($result['response']['results']['publisher']['results'] as $arr) {
    $publishers .= $arr['id'] . ",";
}

EDIT2:根据我自己根据您的代码进行的测试,一切都应该正常工作。不应出现通知/警告。也许您的问题中未显示的其他一些代码导致了它。

于 2013-10-03T22:28:52.570 回答