18

I have this array:

$json = json_decode('
{"entries":[
{"id": "29","name":"John", "age":"36"},
{"id": "30","name":"Jack", "age":"23"}
]}
');

and I am looking for a PHP "for each" loop that would retrieve the key names under entries, i.e.:

id
name
age

How can I do this?

7 Answers 7

39

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

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, I have json file i.e file.json i want to show data in table format tried integrating here is the code <?php $json = file_get_contents("file.json"); $j = json_decode($json); foreach($j as $row) { foreach($row as $key => $val) { echo $key . ': ' . $val; echo '<br>'; } } Here is the error: Warning: Invalid argument supplied for foreach() in /var/www/html/new_git/json.php on line 6
2

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.

4 Comments

This will fetch the actual values. I am looking for the key names. Thanks!
To do that, you'll need the $json object to be an array instead. To do that, you need to supply the second parameter to the json_decode() function
I forgot to add that, you should then use the $json object as an array and use the foreach($json['entries'] as $record){ foreach($record as $key=>$value){...}}
I didn't know that field names could be used as properties (+1). Saved my day!
1

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

Comments

0

You could try getting the properties of the object using get_object_vars:

 $keys = array();

 foreach($json->entries as $entry)
   $keys += array_keys(get_object_vars($entry));

 print_r($keys);

Comments

0
foreach($json->entries[0] AS $key => $name) {
    echo $key;
}

1 Comment

Thanks Mihai. Unfortunately, I am getting this: Fatal error: Cannot use object of type stdClass as array
0
    $column_name =[];
    foreach($data as $i){
        foreach($i as $key => $i){
            array_push($column_name, $key);
        }
        break;
    }

1 Comment

Consider adding a bit of explanation. Code only answers are strongly discouraged!
0

Alternative answer using arrays rather than objects - passing true to json_decode will return an array.

$json = '{"entries":[{"id": "29","name":"John", "age":"36"},{"id": "30","name":"Jack", "age":"23"}]}';
$data = json_decode($json, true);
$entries = $data['entries'];

foreach ($entries as $entry) {
    $id = $entry['id'];
    $name = $entry['name'];
    $age = $entry['age'];
    printf('%s (ID %d) is %d years old'.PHP_EOL, $name, $id, $age);
}

Tested at https://www.tehplayground.com/17zKeQcNUbFwuRjC

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.