Home > OS >  cant access json array using php
cant access json array using php

Time:03-26

how i can access and callback_query->data why i cant access $update->callback_query->data; here is my code:

$update = file_get_contents('php://input');
$update = json_decode($update, true);
$callbak = $update->callback_query->data;

here is the response:

{
    "ok": true,
    "result": [
        {
            "update_id": 167251858,
            "callback_query": {
                "id": 3952005903777779000,
                "from": {
                    "id": 5215115374,
                    "is_bot": false,
                    "first_name": "Task",
                    "last_name": "Paut",
                    "username": "ma2231",
                    "language_code": "en"
                },
                "message": {
                    "message_id": 157,
                    "from": {
                        "id": 5114398973,
                        "is_bot": true,
                        "first_name": "SYsBot",
                        "username": "taskin221"
                    },
                    "chat": {
                        "id": 5215115374,
                        "first_name": "Task",
                        "last_name": "Payout",
                        "username": "ma2231",
                        "type": "private"
                    },
                    "date": 1648165511,
                    "text": "Please enter pin;",
                    "reply_markup": {
                        "inline_keyboard": [
                            [
                                {
                                    "text": 1,
                                    "callback_data": 1
                                },
                                {
                                    "text": 2,
                                    "callback_data": 2
                                },
                                {
                                    "text": 3,
                                    "callback_data": 3
                                }
                            ],
                            [
                                {
                                    "text": 4,
                                    "callback_data": 4
                                },
                                {
                                    "text": 5,
                                    "callback_data": 5
                                },
                                {
                                    "text": 6,
                                    "callback_data": 6
                                }
                            ],
                            [
                                {
                                    "text": 7,
                                    "callback_data": 7
                                },
                                {
                                    "text": 8,
                                    "callback_data": 8
                                },
                                {
                                    "text": 9,
                                    "callback_data": 9
                                }
                            ],
                            [
                                {
                                    "text": "<",
                                    "callback_data": "<"
                                },
                                {
                                    "text": 0,
                                    "callback_data": 0
                                },
                                {
                                    "text": "OK",
                                    "callback_data": "ok"
                                }
                            ]
                        ]
                    }
                },
                "chat_instance": -5726646197225583000,
                "data": 9
            }
        }
    ]
}

CodePudding user response:

Look at documentation:

 json_decode(
    string $json,
    ?bool $associative = null,
    int $depth = 512,
    int $flags = 0
): mixed

and you are passing second argument as true, so it returns array and not object.

And you missing intermediate keys.


So final solution would be:

$update = file_get_contents('php://input');
$update = json_decode($update, true);
$callbak = $update['result'][0]['callback_query']['data'];
  • Related