0

I have an array and I am trying to access the value for message. I think it is throwing me off because it is an array of objects within an object. Below is what I have tried and the data structure.

Code:

foreach ($result as $value) {

         echo "$value[0]->message"; 
}

Array:

stdClass Object
(
    [results] => Array
        (
            [0] => stdClass Object
                (
                    [message] => 12345
                    [unit] => test
                    [createdAt] => 2013-01-21T14:57:26.613Z
                    [updatedAt] => 2013-01-21T14:57:26.613Z
                    [objectId] => 0uiYuJcRYY
                )

        )

)

2 Answers 2

5

Remove quotes

foreach ($result as $value) {

         echo $value[0]->message; 
}
Sign up to request clarification or add additional context in comments.

3 Comments

If their were multiple objects in the list, how would I access each of them? Would it be another foreach statement within the foreach statement?
yes, if $value from your loop contained another array of objects then to print them all you would need to run another loop on those
@JonErickson You are most welcome. I must appreciate that you were almost there yourself, so this is almost your own answer. It was just a matter of quotation marks.
0

There are two pobable mistakes.

1 - Probable the variable are $results and not $result 2 - If you are using foreach, you don't need the [0], because this is the variable $value now.

try

foreach ($results as $value) {
    echo $value->message; 
}

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.