How to read JSON data in php without knowing the key value

I need to read firebase JSON url in php and then display it. My bomb hit below .json data:

{"dDsdE4AlB7P5YYd4fWbYTQKCLPh1":{"email":"abhi@gmail.com","name":"abhishek"},
 "z1ceiLhdh9YVu7lGnVvqDWoWHFH3":{"email":"ravi@gmail.com","name":"ravish"},
 "ghg76JHG8jhkj7GHGJ763kjhFF45":{"email":"John@gmail.com","name":"John"}}

I can access the name field using the key value as shown below: -

$url = 'https://xxxx.firebaseio.com/users.json'; // path to JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$characters = json_decode($data); // decode the JSON feed

echo $characters->dDsdE4AlB7P5YYd4fWbYTQKCLPh1->name;

Now, how to access the "name" field without knowing the key value? Since these keys are automatically generated by firebase and can be any values.

Thank you very much in advance!

+4
source share
2 answers

Make an associative array from your json. Then you can cross it using foreach with keys and values. This way you will have key value and related data.

$characters = json_decode($data, 1);

foreach ($characters as $key => $value) {
    echo $key . ' ' . $echo $value['name'];
}
+6
source

:

array_keys($yourEncodedJson);

.

, .

+1

Source: https://habr.com/ru/post/1686750/


All Articles