2012-09-03 74 views
-1

您好,我實際上是想在第三級用不同的鍵排序數組。PHP ARRAY排序在第三級

以下是樣本數組輸入。

Array 
(
    [19067] => Array 
     (
      [Morrisville, PA] => 5.02 
     ) 

    [18977] => Array 
     (
      [Washington Crossing, PA] => 6.63 
     ) 

    [08695] => Array 
     (
      [Trenton, NJ] => 2.84 
     ) 

    [08690] => Array 
     (
      [Trenton, NJ] => 1 
     ) 

    [08666] => Array 
     (
      [Trenton, NJ] => 2.84 
     ) 

    [08650] => Array 
     (
      [Trenton, NJ] => 0 
     ) 

    [08648] => Array 
     (
      [Lawrence Township, NJ] => 2.88 
     ) 

    [08647] => Array 
     (
      [Trenton, NJ] => 2.84 
     ) 

    [08646] => Array 
     (
      [Trenton, NJ] => 2 
     ) 

) 

任何機構可以幫我在第三級以上數組進行排序

預期結果想:

輸出:

Array 
(
    [08650] => Array 
     (
      [Trenton, NJ] => 0 
     ) 

    [08690] => Array 
     (
      [Trenton, NJ] => 1 
     ) 

    [08646] => Array 
     (
      [Trenton, NJ] => 2 
     ) 

    [08666] => Array 
     (
      [Trenton, NJ] => 2.84 
     ) 

    [08695] => Array 
     (
      [Trenton, NJ] => 2.84 
     ) 

    [08647] => Array 
     (
      [Trenton, NJ] => 2.84 
     ) 

    [08648] => Array 
     (
      [Lawrence Township, NJ] => 2.88 
     ) 

    [19067] => Array 
     (
      [Morrisville, PA] => 5.02 
     ) 

    [18977] => Array 
     (
      [Washington Crossing, PA] => 6.63 
     ) 


) 

在此先感謝..

回答

2
uasort($array, function (array $a, array $b) { 
    return current($a) - current($b); 
}); 

請注意評論:小於1.0的值將按照相同的順序排序。您可能需要執行(current($a) - current($b)) * 100或以其他方式舍入值,具體取決於您期望的值範圍。

+2

東西告訴我,浮點值並不總是適用於比較值...不知道我讀過那裏的內容:/否,+1 –

+0

浮點在這裏很好。你的意思是'$ foo == 1.2345'通常不適用於浮點數,因爲它們的值不能保證精確。 – deceze

+2

發現它,它會隱式投射到整數,所以0.1和0.99比較相同,這可能並不總是正確的:) –