A
A
Aristarchus2018-04-15 00:53:47
PHP
Aristarchus, 2018-04-15 00:53:47

How to read JSON in PHP?

[
  {
    "token": "тут токен",
    "id": "тут айди",
    "hidden_friends": 2,
    "timezone": "Europe/Kaliningrad"
  },
  {
    "token": "тут токен",
    "id": "тут айди",
    "hidden_friends": 0,
    "timezone": "Europe/Paris"
  }
]

Started writing
<?php

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

$id = $data['id'];

echo $id;
?>

and doesn't work

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Immortal_pony, 2018-04-15
@EnderStore

and doesn't work

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question