php – 使用array_multisort对多维数组进行排序

前端之家收集整理的这篇文章主要介绍了php – 使用array_multisort对多维数组进行排序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这个阵列
Array
(
    [0] => Array
        (
            [brand] => blah blah
            [location] => blah blah
            [address] => blah blah
            [city] => blah blah
            [state] => CA
            [zip] => 90210
            [country] => USA
            [phone] => 555-1212
            [long] => -111
            [lat] => 34
            [distance] => 3.08
        )
    [1] => Array
        (
            [brand] => blah blah
            [location] => blah blah
            [address] => blah blah
            [city] => blah blah
            [state] => CA
            [zip] => 90210
            [country] => USA
            [phone] => 555-1212
            [long] => -111
            [lat] => 34
            [distance] => 5
        )
.
.
.

}

我希望能够按距离对哈希中的数组进行排序.救命!非常感谢

您需要先提取所有距离,然后将距离和数据传递给函数.如 array_multisort文档中的示例3所示.
foreach ($data as $key => $row) {
    $distance[$key] = $row['distance'];
}

array_multisort($distance,SORT_ASC,$data);

这假设您首先需要最短距离,否则将SORT_ASC更改为SORT_DESC

原文链接:https://www.f2er.com/php/138831.html

猜你在找的PHP相关文章