0

I have below array how can I get key name in loop?

$value = stdClass Object ( [autoincrement_id] => 1 [ID] => 1 [State] => Gujarat [Country] => India [Company] => Company1 [rowid] => 1 )

echo $value->State; // I Get Gujarat

But how i get Key name dynamic, i have loop and i don't know my key name. What i can write in loop so i get key name in loop?

echo $value->[????] // Which i write here to i get value of state?
ImBhavin95
  • 1,494
  • 2
  • 16
  • 29

1 Answers1

2

You can do it with foreach:

foreach ($values as $key => $val) {
    // similar results
    echo $values->{$key};
    echo $val;
}
Claudio
  • 5,078
  • 1
  • 22
  • 33
  • What is `$values`? I suppose OP asks about object. If object does not implement `Iterator` you cannot iterate over it. – u_mulder Jun 15 '18 at 08:32
  • Object properties can be iterate. But only if the properties are public. Protected and private properties are hidden. – Clemen Jun 15 '18 at 08:57