0

I have a json, and I do a foreach on this json, i have something like this :

object(stdClass) [118]
  public 'value1' => string 'test'
  public 'value2' => string 'test2'
  public 'value3' => string 'test2'
  public 'value4' => string 'test4'

I succeed to take value1 and "value2" etc of all my object, but i want to take too the id of these objects. I want to mean, i want to have the "118" for this example. How can i do it ?

Someone to help me ?

1
  • foreach is what you're looking for Commented Mar 28, 2014 at 14:14

1 Answer 1

1

When iterating over an array you can get both the value of the current item as well as its key by using foreach ($array as $key => $value).

foreach ($objects as $index => $object) {
    print_r($index.' '.$object->value1)
}
Sign up to request clarification or add additional context in comments.

2 Comments

Objects are not arrays. Does foreach work on objects?
First and foremost, yes foreach works on objects by iterating over their public properties (or if they implement special interfaces the iteration does whatever the method implementations do).

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.