排序使用在array_multisort一个多维数组 [英] sort a multidimentional array using array_multisort

查看:132
本文介绍了排序使用在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. HELP! Thanks a lot

推荐答案

您需要先提取所有的距离,然后通过二者的距离和数据的功能。如例3在在array_multisort 文档中

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆