phptelegram-botphp-telegram-bot

php array push adding that data in other code block


i want to array push some data to my main array. but it add my push data incorrectly. i have below code.

$customKeyboard = array();
$customKeyboard = [
    "inline_keyboard" => [
        [["text" => "back 🔙", "callback_data" => "back"]]
    ]
];

then I do this

$customKeyboard[] = [["text" => "test", "callback_data" => "test"]];

and i want to $customKeyboard[] data will result as below

$customKeyboard = [
    "inline_keyboard" => [
        [["text" => "back 🔙", "callback_data" => "back"]],
        [["text" => "test", "callback_data" => "test"]]
    ]
];

Solution

  • You have to properly tell the system under what level / hierarchy you want to add the new data

    So , change the line

    $customKeyboard[] = [["text" => "test", "callback_data" => "test"]];
    

    to

    $customKeyboard["inline_keyboard"][] = [["text" => "test", "callback_data" => "test"]];
    

    So, use the following

    <?php
    $customKeyboard = array();
    $customKeyboard = [
        "inline_keyboard" => [
            [["text" => "back 🔙", "callback_data" => "back"]]
        ]
    ];
    
    $customKeyboard["inline_keyboard"][] = [["text" => "test", "callback_data" => "test"]];
    ?>
    

    See Demo