PHP,正确拆分一个多维数组并循环使用
PHP, properly split a multidimensional array and use it in loop
我坚持这个任务 - 将数组分成两部分并进行处理。这
情况如下:
$selectCity = "select districtName from telegramCity limit 12 offset 0";
$result = mysqli_query($this->connection, $selectCity);
while ($row = mysqli_fetch_assoc($result)) {
$this->buttons[] = [[
"text" => $row["districtName"],
"callback_data" => strval(0)
],[
"text" => $row["districtName"],
"callback_data" => strval(0)
]];
}`
如您所见,有两个相同的数组,每个数组有 12 条记录。但我需要做到每个都有 6 个。拜托,拜托,告诉我怎么做 ><
我不需要重复的数组。我有 12 个城市的列表,我需要将它们放入 Telegram inline_button,其中应该有 2 列,每列有 6 个城市
您可以在 while 循环中获取另一个时间:
$selectCity = "select districtName from telegramCity limit 12 offset 0";
$result = mysqli_query($this->connection, $selectCity);
// 1st fetch
while ($row = mysqli_fetch_assoc($result)) {
$data1 = [
"text" => $row["districtName"],
"callback_data" => strval(0)
];
// prepare an empty array, in case of there is no more results
$data2 = ['text'=>'', 'callback_data'=> strval(0)] ;
// 2nd fetch, and put in $data2
if ($row = mysqli_fetch_assoc($result)) {
$data2["text"] = $row["districtName"];
}
// fill your final array using the 2 arrays
$this->buttons[] = [$data1, $data2];
}
数组 $this->buttons
最多包含 6 个条目。
我坚持这个任务 - 将数组分成两部分并进行处理。这 情况如下:
$selectCity = "select districtName from telegramCity limit 12 offset 0";
$result = mysqli_query($this->connection, $selectCity);
while ($row = mysqli_fetch_assoc($result)) {
$this->buttons[] = [[
"text" => $row["districtName"],
"callback_data" => strval(0)
],[
"text" => $row["districtName"],
"callback_data" => strval(0)
]];
}`
如您所见,有两个相同的数组,每个数组有 12 条记录。但我需要做到每个都有 6 个。拜托,拜托,告诉我怎么做 ><
我不需要重复的数组。我有 12 个城市的列表,我需要将它们放入 Telegram inline_button,其中应该有 2 列,每列有 6 个城市
您可以在 while 循环中获取另一个时间:
$selectCity = "select districtName from telegramCity limit 12 offset 0";
$result = mysqli_query($this->connection, $selectCity);
// 1st fetch
while ($row = mysqli_fetch_assoc($result)) {
$data1 = [
"text" => $row["districtName"],
"callback_data" => strval(0)
];
// prepare an empty array, in case of there is no more results
$data2 = ['text'=>'', 'callback_data'=> strval(0)] ;
// 2nd fetch, and put in $data2
if ($row = mysqli_fetch_assoc($result)) {
$data2["text"] = $row["districtName"];
}
// fill your final array using the 2 arrays
$this->buttons[] = [$data1, $data2];
}
数组 $this->buttons
最多包含 6 个条目。