2015-12-12 59 views
1

的基礎上,我有這樣的排序陣列,並安排一定的價值

[Defense] => Array 
    (
     [0] => stdClass Object 
      (
       [id] => 1 
       [position] => G 
       [point] => 1.6 

      ) 
     [1] => stdClass Object 
      (
       [id] => 1 
       [position] => F 
       [point] => 2.7 
      ) 


     [2] => stdClass Object 
      (
       [id] => 1 
       [position] => C 
       [point] => 1.4 
      ) 
     [3] => stdClass Object 
      (
       [id] => 2 
       [position] => C 
       [point] => 1.7 
      ) 

     [4] => stdClass Object 
      (
       [id] => 2 
       [position] => D 
       [point] => 1.1 
      ) 
     [5] => stdClass Object 
      (
       [id] => 1 
       [position] => F 
       [point] => 1.7 
      ) 

     [6] => stdClass Object 
      (
       [id] => 1 
       [position] => G 
       [point] => 1.6 

      ) 

     [7] => stdClass Object 
      (
       [id] => 1 
       [position] => G 
       [point] => 1.8 

      ) 
     [8] => stdClass Object 
     (
       [id] => 2 
       [position] => D 
       [point] => 1.5 
     ) 
) 

我想點descendingPHP基礎上排序的數組的數組。此外,所有的位置順序來(全部G比所有F等..) 這樣的:

[Defense] => Array 
     (
      [0] => stdClass Object 
       (
        [id] => 1 
        [position] => G 
        [point] => 1.8 

       ) 
      [1] => stdClass Object 
       (
        [id] => 1 
        [position] => G 
        [point] => 1.6 

       ) 
      [2] => stdClass Object 
       (
        [id] => 1 
        [position] => G 
        [point] => 1.6 

       ) 
      [3] => stdClass Object 
       (
        [id] => 1 
        [position] => F 
        [point] => 2.7 
       ) 
      [4] => stdClass Object 
       (
        [id] => 1 
        [position] => F 
        [point] => 1.7 
       ) 

      [5] => stdClass Object 
       (
        [id] => 1 
        [position] => C 
        [point] => 1.7 
       ) 
      [6] => stdClass Object 
       (
        [id] => 2 
        [position] => C 
        [point] => 1.4 
       ) 
      [7] => stdClass Object 
       (
        [id] => 2 
        [position] => D 
        [point] => 1.5 
       ) 
      [8] => stdClass Object 
       (
        [id] => 2 
        [position] => D 
        [point] => 1.1 
       ) 

    ) 

任何幫助嗎? )`usort(`或`uasort()`:

+0

我說看看PHPS陣列排序功能進行排序。 – arkascha

+0

構建排序算法的邏輯或使用內置數組排序功能。將數組的「點或位置」變量傳遞給排序函數,並對它們進行排序。 –

回答

1

使用usort()函數到陣列如下面

usort($array,function($a,$b){ 
if($a->position == $b->position) 
    return $a->point < $b->point; 
else 
    return $a->position < $b->position; 
}); 
var_dump($array); 
+0

感謝您的回覆傢伙.. @山姆您發送的代碼是按位置安排,但不在'點'的基礎上進行排序我想在點'降'的基礎上排序 – Rahul

+0

運行我寫的上述功能,它首先檢查點,如果點是平等的,它使用位置排序,否則它使用點,這是默認的,我建議你運行它在你的PHP腳本,我已經運行它並顯示完美的結果 –

+0

我想排序首先以位置爲基礎,然後對於這些位置,這些點將按DESC順序排列。我希望它是有道理的 – Rahul