为最高纬度和经度排序多维数组

时间:2011-11-01 14:04:44

标签: php arrays sorting multidimensional-array

我一直试图解决这个问题。我有一个位置数组,以及相对于该位置的子数组值都来自xml文件。例如:

Array
(
    [ab15] => Array
        (
            [stid] => ab15
            [name] => Alan Briggs
            [stnm] => 2072
            [lat] => 46.9814
            [lon] => -67.4305
            [elev] => 0
            [net] => NBP
            [ecrg] => nil
        )

    [alw] => Array
        (
            [stid] => alw
            [name] => Alward
            [stnm] => 2000
            [lat] => 46.2
            [lon] => -65.445
            [elev] => 123
            [net] => FORET
            [ecrg] => NAN
        )

    [cwzs] => Array
        (
            [stid] => cwzs
            [name] => Amqui
            [stnm] => 489
            [lat] => 48.4722
            [lon] => -67.4342
            [elev] => 165
            [net] => DEA
            [ecrg] => NAN
        )
...

我需要按顺序对它们进行排序,以便最高['lat']和最低['lon']成为第一,依此类推。例如:

Array
(

    [cwzs] => Array
        (
            [stid] => cwzs
            [name] => Amqui
            [stnm] => 489
            **[lat] => 48.4722
            [lon] => -67.4342**
            [elev] => 165
            [net] => DEA
            [ecrg] => NAN
        )

    [ab15] => Array
        (
            [stid] => ab15
            [name] => Alan Briggs
            [stnm] => 2072
            [lat] => 46.9814
            [lon] => -67.4305
            [elev] => 0
            [net] => NBP
            [ecrg] => nil
        )

    [alw] => Array
        (
            [stid] => alw
            [name] => Alward
            [stnm] => 2000
            [lat] => 46.2
            [lon] => -65.445
            [elev] => 123
            [net] => FORET
            [ecrg] => NAN
        )

...

我不知道如何解决这个问题,我看了一系列的数组排序函数,我似乎无法解决这个问题。

1 个答案:

答案 0 :(得分:2)

uasort($arr, function($item1, $item2)
{
    if($item1['lat'] == $item2['lat'])
    {
       if($item1['lon'] == $item2['lon'])
       {
           return 0;
       }
       return $item1['lon'] > $item2['lon'] ? 1 : -1;
    }
    return $item1['lat'] > $item2['lat'] ? -1 : 1;
});