首页 > 解决方案 > 从mysql数据的数组中添加数组

问题描述

我需要这个代码。您可以通过复制测试代码。代码已准备好工作。只需要很少的变化。

我有一个准备好的数组。它必须以这样一种方式进行准备,即生成的电报在放置在机器人上时必须能够正常工作。每个第 3 个元素必须创建新数组并放入该数组中。这是为了确保每 3 个项目后继续一个新行

function getMarkets(){
    $arr = [];
    $query = Array(
        ["text" => "One", "callback_data" => "one"],
        ["text" => "Two", "callback_data" => "two"],
        ["text" => "Three", "callback_data" => "three"],
        ["text" => "Four", "callback_data" => "four"],
        ["text" => "Five", "callback_data" => "five"],
        ["text" => "Six", "callback_data" => "six"],
        ["text" => "Seven", "callback_data" => "seven"],
    );
    foreach($query as $key => $val){
        $arr += [$key => $val];
        if($key % 2 === 1) {
            // every 3th element must create new array and put inside that array
        }
    }
    // $arr += ["text" => "Back", "callback_data" => "back"];  /// must add this last of array
    return $arr;
}

$needMakeArray= array(
    'inline_keyboard' => [
        getMarkets()
    ]
);

$readyArray= array(
    'inline_keyboard' => [
        [
            ['text' =>  "and", 'callback_data' => "andijon"],
            ['text' =>  "nam", 'callback_data' => "namangan"],
            ['text' =>  "tosh", 'callback_data' => "toshkent"],
        ],
        [
            ['text' =>  "far", 'callback_data' => "fargona"],
            ['text' =>  "sam", 'callback_data' => "samarqand"],
            ['text' =>  "bux", 'callback_data' => "buxoro"],
        ],
        [
            ['text' =>  "qash", 'callback_data' => "qashqadaryo"],
            ['text' =>  "sur", 'callback_data' => "surxondaryo"],
            ['text' =>  "xor", 'callback_data' => "xorazm"],
        ],
        [
            ['text' =>  "qor", 'callback_data' => "qoraqalpogiston"],
            ['text' =>  "jiz", 'callback_data' => "jizzax"],
            ['text' =>  "sir", 'callback_data' => "sirdaryo"],
        ],
        [
            ['text' =>  "nav", 'callback_data' => "navoiy"],
            ['text' =>  "toshv", 'callback_data' => "toshkentv"],
        ]
    ]
);


echo '<pre>';
print_r($needMakeArray);
echo '</pre>';


echo '<pre>';
print_r($readyArray);
echo '</pre>';

标签: arrayskeyboardtelegram-bot

解决方案


推荐阅读