M
M
Max Darkleviathan2019-10-05 09:47:20
PHP
Max Darkleviathan, 2019-10-05 09:47:20

How to parse multidimensional json php?

I'm trying to parse this json:

{
  "ok": true,
  "result": [
    {
      "message": {
        "date": 1270110569,
        "text": "hello",
        "from": {
          "first_name": "Test",
          "last_name": "FGHTZX",
          "is_bot": false,
          "id": 234512896,
          "language_code": "ru"
        },
        "message_id": 16,
        "chat": {
          "first_name": "Test",
          "last_name": "FGHTZX",
          "type": "private",
          "id": 234512896
        }
      },
      "update_id": 474148724
    }
  ]
}

I want to get the values ​​of text and id
There are no problems with simpler ones, but specifically with this I can’t at least get the values ​​​​directly, writes array
Tried like this:
$string = file_get_contents($json_file);

$result_parse = json_decode($string, TRUE);

$r=$result_parse->result->message->text;

print "$r";

Tried the same
$r=$result_parse['result'];

print "$r";

output Array, although if $result_parse['ok'] was accessed it outputs 1, which corresponds to true

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Derepko, 2019-10-05
@darkleviathan

"result": [means that the array will go now. those. result in this case is an array
"message": {means that the object will go now. those. message in this case is an object.
In your case

$string = file_get_contents($json_file);

$result_parse = json_decode($string, TRUE);

$r=$result_parse->result[0]->message->text;

print "$r";

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question