0

I have the following part of PHP object class:

stdClass Object ( 
[data] => Array ( 
[0] => stdClass Object ( 
[id] => 1194760 

.....

I am trying to extract all values for id. When I echo $cronJobUrlList->data[0]->id I do get a value but only for the first. When I try the following nothing is returned.

foreach ($cronJobUrlList->data as $data) {
    foreach ($data->id as $cronId) {
        echo $cronId;
    }
}

I would like to obtain the id value for every entry. I never worked with object classes before.

2
  • 1
    Shouldn't it be foreach ($cronJobUrlList->data as $data) { echo $data->id; } ? Commented Sep 15, 2015 at 13:41
  • @AlanMachado it worked! Thanks. Commented Sep 15, 2015 at 13:43

3 Answers 3

1

You are trying to access an object inside the array index. Once you iterate over the array, every instance will return an object and you only need to call for its property, so you don't have to iterate over the value again.

foreach ($cronJobUrlList->data as $data) {
    echo $data->id;
}
Sign up to request clarification or add additional context in comments.

Comments

1

try this

foreach ($cronJobUrlList->data as $key=> $data) {
       echo $data->id;
}

Comments

0

I can suggest you convert your object to an array using json_encode().

And then decode using json_decode().

From there you can get your id key easily.

The second argument to json_decode forces the JSON to unserialize to an associative array if it is truthy (it is falsy by default).

That is, use json_decode($json, true)

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.