从 mysql 生成一个 php 数组并希望使用数据库中的 ID 作为数组键
Generating a php array from mysql and want to use ID from db as array keys
当前输出是..
Array
(
[0] => Array
(
[ID] => 20
[name] => Peter
[datetime] => Tuesday 26th Oct 21 3:50am
)
[1] => Array
(
[ID] => 21
[name] => Paul
[datetime] => Tuesday 26th Oct 21 4:44am
)
)
我希望数组输出为..
Array
(
[20] => Array
(
[ID] => 20
[name] => Peter
[datetime] => Tuesday 26th Oct 21 3:50am
)
[21] => Array
(
[ID] => 21
[name] => Paul
[datetime] => Tuesday 26th Oct 21 4:44am
)
)
我目前用来生成数组的代码是..
$sql=mysqli_query($conn,"SELECT * FROM `live`");
/*every time it fetches the row, adds it to array...*/
while($liveuserdata[]=mysqli_fetch_array($sql, MYSQLI_ASSOC));
我无法向您展示我尝试过的内容,因为我不知道从哪里开始尽管进行了几次改写的搜索:-/
就这么简单:
$sql = mysqli_query($conn,"SELECT * FROM `live`");
$liveuserdata = [];
while ($row = mysqli_fetch_array($sql, MYSQLI_ASSOC)) {
$liveuserdata[$row['ID']] = $row;
}
当前输出是..
Array
(
[0] => Array
(
[ID] => 20
[name] => Peter
[datetime] => Tuesday 26th Oct 21 3:50am
)
[1] => Array
(
[ID] => 21
[name] => Paul
[datetime] => Tuesday 26th Oct 21 4:44am
)
)
我希望数组输出为..
Array
(
[20] => Array
(
[ID] => 20
[name] => Peter
[datetime] => Tuesday 26th Oct 21 3:50am
)
[21] => Array
(
[ID] => 21
[name] => Paul
[datetime] => Tuesday 26th Oct 21 4:44am
)
)
我目前用来生成数组的代码是..
$sql=mysqli_query($conn,"SELECT * FROM `live`");
/*every time it fetches the row, adds it to array...*/
while($liveuserdata[]=mysqli_fetch_array($sql, MYSQLI_ASSOC));
我无法向您展示我尝试过的内容,因为我不知道从哪里开始尽管进行了几次改写的搜索:-/
就这么简单:
$sql = mysqli_query($conn,"SELECT * FROM `live`");
$liveuserdata = [];
while ($row = mysqli_fetch_array($sql, MYSQLI_ASSOC)) {
$liveuserdata[$row['ID']] = $row;
}