如何从以下mysql查询直接获得多维数组?
$query = SELECT * FROM table WHERE field1 = 1;
我尝试了以下内容:
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_array($result)){
$table[] = ($row['field1'].'~'.$row['field2'].'~'.$row['field3'].'~'.$row['field4']);
}
如果我们打印它,我们有以下结果:
array(
[0] => 1 ~ ALFA ~ beta ~ 57
[1] => 1 ~ BETA ~ gamma ~ 18
[2] => 1 ~ GAMMA ~ delta ~ 24
...
[999] => 1 ~ ZETA ~ theta ~ 19
)
然后我必须将它爆炸以获得我希望的多维数组:
foreach ($table as $value) {
$tableWithSubArrays[] = explode("~", $value);
}
然后我们得到了它:
Array(
[0] => Array
(
[0] => 1
[1] => ALFA
[2] => beta
[3] => 57
)
[1] => Array
(
[0] => 1
[1] => BETA
[2] => gamma
[3] => 18
)
...
[999] => Array
(
[0] => 1
[1] => ZETA
[2] => theta
[3] => 19
)
有没有其他方法可以直接从查询到多维数组?
非常感谢提前!
答案 0 :(得分:1)
while ($row = mysqli_fetch_array($result)){
$table[] = $row;
}