Question

I have the following stdObject obtained thru cURL / json_decode():

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
                )

        )

)

I'm looping through it in order to obtain each result's ID:

foreach ($result->response->results->publisher->results as $object) {
    $publishers .= $object->id.",";
}

And although the code is working fine, PHP is rising the following notices/warnings:

PHP Notice: Trying to get property of non-object in /var/www/vhosts/domain.net/script.php on line 1 PHP Notice: Trying to get property of non-object in /var/www/vhosts/domain.net/script.php on line 1 PHP Warning: Invalid argument supplied for foreach() in /var/www/vhosts/domain.net/script.php on line 1

Any ideas? Thanks in advance!

Was it helpful?

Solution

Is it possible you have set the second parameter $assoc in json_decode() to true? Like this:

$result = json_decode($json_from_curl, true);

Thereby, getting back $result with all stdClass Objects converted to associative array.

EDIT: If the $result you are getting is indeed an associative array, then we should treat it as such. Try replacing your foreach with this:

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

EDIT2: From my own testing based on your code, everything should be working correctly. The notices/warnings should not occur. Perhaps some other code not shown in your question is causing it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top