Retrieving array keys from JSON input Retrieving array keys from JSON input json json

Retrieving array keys from JSON input


Try it

foreach($json->entries as $row) {    foreach($row as $key => $val) {        echo $key . ': ' . $val;        echo '<br>';    }}

In the $key you shall get the key names and in the val you shal get the values


You could do something like this:

foreach($json->entries as $record){    echo $record->id;    echo $record->name;    echo $record->age;}

If you pass true as the value for the second parameter in the json_decode function, you'll be able to use the decoded value as an array.


I was not satisfied with other answers so I add my own. I believe the most general approach is:

$array = get_object_vars($json->entries[0]);foreach($array as $key => $value) {  echo $key . "<br>";}

where I used entries[0] because you assume that all the elements of the entries array have the same keys.

Have a look at the official documentation for key: http://php.net/manual/en/function.key.php