gpt4 book ai didi

PHP MySQL 排序多维数组

转载 作者:行者123 更新时间:2023-11-29 04:53:28 26 4
gpt4 key购买 nike

所以这让我抓狂。我需要从 mySQL 中提取我的表行,然后对它们进行排序,但随后我需要将它们作为单个数组输出回来。主要是因为这之后的代码是为了接受它而编写的。

这是我的代码。如果您有任何建议,请告诉我。

<?php

include 'connect.php';


$query = mysql_query("SELECT * FROM users");

while ($data = mysql_fetch_assoc($query))
{
$newarray[]=$data;
$dbusername = $data['username'];
$dbpassword = $data['password'];
$logid = $data['id'];
print_r ($data);
echo "<br/>";
}

foreach ($newarray as $key => $row) {
$volume[$key] = $row['password'];
}

array_multisort($volume, SORT_ASC, $newarray);
print_r($newarray);

?>

结果是:

Array ( 

[0] => Array ( [id] => 4 [rating] => 18 [password] => 1981 [username] => 212060064)
[1] => Array ( [id] => 2 [rating] => 6 [password] => 1983 [username] => 212060062)
[2] => Array ( [id] => 3 [rating] => 5 [password] => 1984 [username] => 212060063)
[3] => Array ( [id] => 1 [rating] => 3 [password] => 1988 [username] => 212060061)

)

但是我需要像这样输出它们:

Array ( [id] => 4 [rating] => 18 [password] => 1981 [username] => 212060064)
Array ( [id] => 2 [rating] => 6 [password] => 1983 [username] => 212060062)
Array ( [id] => 3 [rating] => 5 [password] => 1984 [username] => 212060063)
Array ( [id] => 1 [rating] => 3 [password] => 1988 [username] => 212060061)

最佳答案

根据您的发言:

The result of this is:

Array ( 

[0] => Array ( [id] => 4 [rating] => 18 [password] => 1981 [username] => 212060064)
[1] => Array ( [id] => 2 [rating] => 6 [password] => 1983 [username] => 212060062)
[2] => Array ( [id] => 3 [rating] => 5 [password] => 1984 [username] => 212060063)
[3] => Array ( [id] => 1 [rating] => 3 [password] => 1988 [username] => 212060061)

)

However I need to output them like this:

Array ( [id] => 4 [rating] => 18 [password] => 1981 [username] => 212060064)
Array ( [id] => 2 [rating] => 6 [password] => 1983 [username] => 212060062)
Array ( [id] => 3 [rating] => 5 [password] => 1984 [username] => 212060063)
Array ( [id] => 1 [rating] => 3 [password] => 1988 [username] => 212060061)

所以在SORT的方式上没有发现区别。

在最后一行代码中,替换:

print_r($newarray);

通过:

foreach($newarray as $child_array)
{
print_r($child_array);
}

关于PHP MySQL 排序多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9040930/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com