使用array_multisort对多维数组进行排序 [英] sort a multidimensional array using array_multisort
本文介绍了使用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
)
.
.
.
}
我希望能够按距离对散列中的数组进行排序.
I want to be able to sort the arrays in the hash by distance.
推荐答案
您需要先提取所有距离,然后将距离和数据都传递给函数.如 array_multisort 文档中的示例3所示.
You need to extract all the distances first, then pass both the distance and the data to the function. As shown in example 3 in the array_multisort documentation.
foreach ($data as $key => $row) {
$distance[$key] = $row['distance'];
}
array_multisort($distance, SORT_ASC, $data);
这假设您首先要最短的距离,否则将SORT_ASC
更改为SORT_DESC
This assumes you want the shortest distances first, otherwise change the SORT_ASC
to SORT_DESC
这篇关于使用array_multisort对多维数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文