1

I'm trying to read an array and echo one of the variables in the array, like this:

foreach($result->http_response_body as $obj)    {
echo $obj->items['address'];
}

The array looks like this when i print it:

    stdClass Object
(
[http_response_body] => stdClass Object
    (
        [total_count] => 1322
        [items] => Array
            (
                [0] => stdClass Object
                    (
                        [created_at] => Thu, 15 Jan 2015 08:35:06 GMT
                        [tag] => *
                        [id] => 54b77bba55edfbe50e662d3a
                        [address] => [email protected]
                    )

                [1] => stdClass Object
                    (
                        [created_at] => Thu, 15 Jan 2015 08:34:40 GMT
                        [tag] => *
                        [id] => 54b77ba055edfbe50e662cf6
                        [address] => [email protected]
                    )

                [2] => stdClass Object
                    (
                        [created_at] => Thu, 15 Jan 2015 08:31:56 GMT
                        [tag] => *
                        [id] => 54b77afc55edfbe50e662b0d
                        [address] => [email protected]
                    )

But with my script i het this message:

Notice: Trying to get property of non-object in /home/xxxxxxxx/domains/xxxxxxx.xx/public_html/MailGun/watchUnsubscribes.php on line 27

What did I do wrong?

2 Answers 2

3

You need to change the code to this:

foreach ($result->http_response_body->items as $obj) {
    echo $obj->address;
}
Sign up to request clarification or add additional context in comments.

Comments

-2

Do this instead:

foreach($result->http_response_body->items as $obj)    {
echo (string)$obj->address;
}

1 Comment

The provided code does not solve the problem as the author wants all response items, not just the first one.

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.