2013-01-04 97 views
4
我有一個陣列這樣

號碼,排序陣列使用內陣列中在PHP或MySQL

陣列[0] [0],[1] [0],[2] [0]是問題,並在數組[0] [1],[1] [1],[2] [1]是對這些問題的答案。
現在我想對這個數組的數量進行排序。
第一個問題只有一個答案和 第二個有兩個答案和第三個有三個答案。
對於該問題的答案數,排序後的數組應該按降序排列。 是否有無法在sql中實現這一點?或者我們如何在PHP中排序?希望我的問題很明確。先謝謝你。

Array 
(
    [0] => Array 
     (
      [0] => Array 
       (
        [0] => 12 
        [1] => 110 
        [2] => Tom 
        [3] => [email protected] 
        [4] => Hello tom how are you? 
        [5] => 2013-01-03 14:54:07 
       ) 

      [1] => Array 
       (
        [0] => Array 
         (
          [0] => 12 
          [1] => Yes i am fine. 
          [2] => Arun 
          [3] => [email protected] 
          [4] => 2013-01-03 14:55:22 
         ) 
       ) 
     ) 
    [1] => Array 
     (
      [0] => Array 
       (
        [0] => 12 
        [1] => 110 
        [2] => Tom 
        [3] => [email protected] 
        [4] => Hello tom how are you. 
        [5] => 2013-01-03 14:54:07 
       ) 

      [1] => Array 
       (
        [0] => Array 
         (
          [0] => 12 
          [1] => Yes i am fine 
          [2] => Arun 
          [3] => [email protected] 
          [4] => 2013-01-03 14:55:22 
         ) 
        [1] => Array 
         (
          [0] => 12 
          [1] => Yes i am fine 
          [2] => Arun 
          [3] => [email protected] 
          [4] => 2013-01-03 14:55:22 
         ) 
       ) 
     ) 
    [2] => Array 
     (
      [0] => Array 
       (
        [0] => 12 
        [1] => 110 
        [2] => Tom 
        [3] => [email protected] 
        [4] => Hello tom how are you. 
        [5] => 2013-01-03 14:54:07 
       ) 
      [1] => Array 
       (
        [0] => Array 
         (
          [0] => 12 
          [1] => Yes i am fine 
          [2] => Arun 
          [3] => [email protected] 
          [4] => 2013-01-03 14:55:22 
         ) 
        [1] => Array 
         (
          [0] => 12 
          [1] => Yes i am fine 
          [2] => Arun 
          [3] => [email protected] 
          [4] => 2013-01-03 14:55:22 
         ) 
       [2] => Array 
         (
          [0] => 12 
          [1] => Yes i am fine 
          [2] => Arun 
          [3] => [email protected] 
          [4] => 2013-01-03 14:55:22 
         ) 
       ) 
     ) 
+0

究竟是什麼,你是想用這個做什麼? – 2013-01-04 05:54:12

+0

按降序對問題進行排序,以便首先顯示具有最多答案的用戶。你是否清楚我的問題? –

+0

當您將數據導出到SO時,最好使用'var_export',這樣人們可以將您提供的數據複製到測試中。 – Supericy

回答

4

這應該工作:

function cmp($a, $b) 
{ 
    return count($a[1]) > count($b[1]) ? -1 : 1; 
} 

usort($data, 'cmp'); 

或者,如果你正在使用PHP 5.3.0或更高,則可以使用:

usort($data, function ($a, $b) { 
    return count($a[1]) > count($b[1]) ? -1 : 1; 
}); 
+0

+1 - Thankyou Supericy。它的工作正常。我需要更多的時間來理解和測試你的答案。 –