gpt4 book ai didi

php - 按递增顺序排列多排序数字

转载 作者:太空宇宙 更新时间:2023-11-03 12:15:50 26 4
gpt4 key购买 nike

以下代码是否可以按 commentID 升序对数组中的数据进行排序?

我现在无法测试代码,因为我的数据库中没有测试值。

$comments = array();

---insert mysql data into $comments here---

$tmp = Array();
foreach($comments as &$ma)
$tmp[] = &$ma["commentID"];
array_multisort($tmp, $comments);

谢谢!

最佳答案

我会按照以下方式进行:

$comments = array();
// Use your preferred mysql driver here
$result = $mysqli->query($query);
while ($row = $result->fetch_assoc()){
$comments[$row["id"]] = $row["comment"];
}
$success = ksort($comments);

ksort 将根据键从低到高对数组进行排序。在这种情况下,键是数据库中的 id。如果不清楚,或者这不是您想要的,请告诉我。

关于php - 按递增顺序排列多排序数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22265109/

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