arrays - 从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>';
解决方案
推荐阅读
- java - 在空对象引用上接收 'android.content.Context android.content.Context.getApplicationContext()'
- c# - Visual Studio 2019 XAML 设计器不显示来自外部项目的 x64 用户控件
- javascript - 如何在应用程序的同一页面中获取所有活动用户
- node.js - 如何使异步函数在nodejs中作为同步运行
- c - 不可变的 julia 结构可以安全地进行变异吗?在什么条件下可以安全地进行变异?
- python - 自己类中的类属性
- c++ - 在 C++20 中使用 bit_cast 将整数转换为 GLvoid* 是否有效?
- c# - 通过 C# 使用 WebForms 如何初始化/创建在整个应用程序中使用的变量?
- android - 如何只获得最大数量的指针
- javascript - 从初始化定位节点