How to read JSON in PHP?

[
{
 "token": "token here",
 "id": "ID here",
 "hidden_friends": 2,
 "timezone": "Europe/Kaliningrad"
},
{
 "token": "token here",
 "id": "ID here",
 "hidden_friends": 0,
 "timezone": "Europe/Paris"
}
]


He began to write
<?php

$users = file_get_contents('users.json');
$data = json_decode($users, true);

$id = $data['id'];

echo $id;
?>

not working
June 6th 19 at 10:22
1 answer
June 6th 19 at 10:24
Solution
not working

... and it shouldn't. In the array "data" you have no element with the key "id". There are elements with the keys "0" and "1", each of which contains an array and, in turn, have an element with the key "id".
Accordingly, either apply directly to a certain element:
$id= $data[0]['id'];
... or iterate over all the inner arrays:
foreach ($data as $datum) {
 $id = $datum['id'];
}

Find more questions by tags PHP