0

i have a json a object array from which i want to read [message] i tried it to do with the help of foreach loop but it gives my error about

Undefined property: stdClass::$name in C:\xampp\htdocs\fb\select-action.php on line 56

here is the line 56

foreach($data->data as $obj)
  {
    echo   $obj->message.'<br>';
  }

the $data variable which contains the object json array shows this type of results

stdClass Object
(
    [data] => Array
        (
            [0] => stdClass Object
                (
                    [comments] => stdClass Object
                        (
                            [data] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [created_time] => 2017-09-19T09:17:24+0000
                                            [from] => stdClass Object
                                                (
                                                    [name] => Hasni_Collections786
                                                    [id] => 581985938822055
                                                )

                                            [message] => :-p ;)
                                            [id] => 737576403263007_737576629929651
                                        )

                                    [1] => stdClass Object
                                        (
                                            [created_time] => 2017-09-24T12:54:28+0000
                                            [from] => stdClass Object
                                                (
                                                    [name] => Hasni_Collections786
                                                    [id] => 581985938822055
                                                )

                                            [message] => :-p
                                            [id] => 737576403263007_740798849607429
                                        )

                                    [2] => stdClass Object
                                        (
                                            [created_time] => 2017-09-24T12:54:31+0000
                                            [from] => stdClass Object
                                                (
                                                    [name] => Hasni_Collections786
                                                    [id] => 581985938822055
                                                )

                                            [message] => :-p
                                            [id] => 737576403263007_740798896274091
                                        )

                                )

                            [paging] => stdClass Object
                                (
                                    [cursors] => stdClass Object
                                        (
                                            [before] => WTI5dGJXVnVkRjlqZAFhKemIzSTZAOek0zTlRjMk5qSTVPVEk1TmpVeE9qRTFNRFU0TVRJMk5EUT0ZD
                                            [after] => WTI5dGJXVnVkRjlqZAFhKemIzSTZAOelF3TnprNE9EazJNamMwTURreE9qRTFNRFl5TlRjMk56RT0ZD
                                        )

                                )

                        )

                    [id] => 581985938822055_737576403263007
                )

        )

    [paging] => stdClass Object
        (
            [cursors] => stdClass Object
                (
                    [before] => Q2c4U1pXNTBYM0YxWlhKNVgzTjBiM0o1WDJsa0R5UTFPREU1T0RVNU16ZAzRNakl3TlRVNkxUZAzNPRE0xTnpJNU9UQTVOekExTVRVNE56SVBER0ZA3YVY5emRHOXllVjlwWkE4ZAk5UZA3hPVGcxT1RNNE9ESXlNRFUxWHpjek56VTNOalF3TXpJMk16QXdOdzhFZAEdsdFpRWlp3T0I4QVE9PQZDZD
                    [after] => Q2c4U1pXNTBYM0YxWlhKNVgzTjBiM0o1WDJsa0R5UTFPREU1T0RVNU16ZAzRNakl3TlRVNkxUZAzNPRE0xTnpJNU9UQTVOekExTVRVNE56SVBER0ZA3YVY5emRHOXllVjlwWkE4ZAk5UZA3hPVGcxT1RNNE9ESXlNRFUxWHpjek56VTNOalF3TXpJMk16QXdOdzhFZAEdsdFpRWlp3T0I4QVE9PQZDZD
                )

        )

)
3
  • Do you realize that as soon as it is an object array it is no longer Json? If you've verified that the contents of the array is good, the fact that it once was serialized Json is irrelevant. Commented Sep 24, 2017 at 14:33
  • Could you edit your queston to include the JSON string. Commented Sep 24, 2017 at 14:36
  • how mad i am. sorry to put you in such a trouble. but my question remain same i want to fetch the value which is located at message index. Commented Sep 24, 2017 at 14:36

1 Answer 1

2

Your response seems to be a mixed Array/stdObject. If your given output is from $data, your foreach would have to look like that:

foreach ($data->data[0]->comments->data as $obj) {
    echo $obj->message.'<br>';
}

Is there somewhere a json_decode()? It seems some parts are casted to array or something like it. Consider using json_decode($json, true), this would give you the whole result as an array, without this strange mixup.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.