2012-12-01 28 views
-1

可能重複:
How to sort the following PHP multidimensional array?排序多維數組在每個級別分別

比方說,我有以下陣列:

Array 
    (
     [0] => Array 
      (
       [id] => 5 
       [name] => Education 
       [parent] => 1 
       [sort_order] => 3 
       [leaf] => 1 
      ) 

     [1] => Array 
      (
       [id] => 6 
       [name] => Computers 
       [parent] => 1 
       [sort_order] => 1 
       [leaf] => 1 
      ) 

     [2] => Array 
      (
       [id] => 7 
       [name] => Science 
       [parent] => 1 
       [sort_order] => 2 
       [children] => Array 
        (
         [0] => Array 
          (
           [id] => 8 
           [name] => Mathematics 
           [parent] => 7 
           [sort_order] => 2 
           [children] => Array 
            (
             [0] => Array 
              (
               [id] => 11 
               [name] => Geometry 
               [parent] => 8 
               [sort_order] => 2 
               [leaf] => 1 
              ) 

             [1] => Array 
              (
               [id] => 12 
               [name] => Algebra 
               [parent] => 8 
               [sort_order] => 1 
               [leaf] => 1 
              ) 

            ) 

          ) 

         [1] => Array 
          (
           [id] => 9 
           [name] => Physics 
           [parent] => 7 
           [sort_order] => 1 
           [leaf] => 1 
          ) 

         [2] => Array 
          (
           [id] => 10 
           [name] => Biology 
           [parent] => 7 
           [sort_order] => 4 
           [leaf] => 1 
          ) 

         [3] => Array 
          (
           [id] => 13 
           [name] => History 
           [parent] => 7 
           [sort_order] => 3 
           [leaf] => 1 
          ) 

        ) 

      ) 

是否有任何PHP內置函數它將按照'sort_order'的值分別在每個級別上對這個數組進行排序?我找到了幾個解決方案,但是它們都按字母順序排序或者使用了ksort()。

可能會有遞歸需要,但我需要一些幫助如何開始。 TIA。

+0

重複情況應該發生什麼?你不喜歡字母表,因爲10會在2之前出現,或者是什麼? – Ally

+0

@Ally我正在防止重複的情況。每個級別上的'sort_order'都是唯一的。 – user1292810

+1

[How to sort the following PHP multidimensional array?](http://stackoverflow.com/questions/12900539/how-to-sort-the-following-php-multidimensional-array)或http:// stackoverflow .com/questions/10998830/sort-php-multidimensional-array-by-value or http://stackoverflow.com/questions/13613286/sorting-a-big-multidimensional-array或http://stackoverflow.com/questions/13148243/sort-multi-dimensional-array-php or http://stackoverflow.com/questions/12006771/sort-multi-dimensional-array,http://stackoverflow.com/questions/12450304/sort-multidimensional-array -data-into-numerical-orde – PeeHaa

回答

0

有一個:usort它允許你構造比較函數。

排序1級別

什麼是比較函數?一個函數帶兩個參數,當第一個比第二個低時返回-1,第一個比較大時返回+1,如果兩個都相等則返回0。

如何定義函數?

如果有內置函數或個人函數帶來函數性,那麼表示函數名稱的字符串就是該點。

,如果它是一個對象的方法,你必須使用一個陣列,通過實例的引用和方法名

,並至少,你可以使用匿名函數是這樣的:

function sortALevel(&$array){ 

usort($array,function($a,$b){ 
    if($a['sort_order'] > $b['sort_order']){ 
     return 1; 
    } 
    if($a['sort_order'] < $b['sort_order']){ 
     return -1; 
    } 
    return 0; 

    }); 
    } 

排序整個陣列

function sort_recursive($array){ 
sortALevel($array); 
foreach($array as $value){ 
    if(is_array($value)){ 

    sort_recursive($value); 
    } 
} 
} 
+0

如果數組的級別比前兩個級別更深,則不會起作用。 – Ally

+0

是的,我的第二個片段不好 – artragis

+0

現在是。對不起。 – artragis