2015-08-28 57 views
-2

我想按值P對數組進行排序,如果兩個P相同,則按PD進行排序,如果兩個PD相同,則按與PA相同的PF進行排序。
我如何實現這一目標?在PHP中使用多個值排序多維數組

我有多維數組在PHP就像

PHP:

 
$abc = array(
    array('id' => 1,'name' => 'abc','overall' => array('MP' => '2','W'=> '2','L' => '0','D'=> '0','PF' => '2904','PA' => '1932','PD' => '972','P' => '6')), 
    array('id' => 1,'name' => 'abc','overall' => array('MP' => '2','W'=> '1','L' => '1','D'=> '0','PF' => '2320','PA' => '1974','PD' => '346','P' => '3')), 
    array('id' => 1,'name' => 'abc','overall' => array('MP' => '2','W'=> '1','L' => '1','D'=> '0','PF' => '1620','PA' => '1824','PD' => '-204','P' => '3')), 
    array('id' => 1,'name' => 'abc','overall' => array('MP' => '2','W'=> '0','L' => '2','D'=> '0','PF' => '2200','PA' => '2300','PD' => '-100','P' => '0')) 
); 

HTML:

 
Array 
(
    [0] => Array 
     (
      [id] => 1 
      [name] => abc 
      [overall] => Array 
       (
        [MP] => 2 
        [W] => 0 
        [L] => 2 
        [D] => 0 
        [PF] => 2200 
        [PA] => 2300 
        [PD] => -100 
        [P] => 0 
       ) 

     ) 

    [1] => Array 
     (
      [id] => 1 
      [name] => abc 
      [overall] => Array 
       (
        [MP] => 2 
        [W] => 1 
        [L] => 1 
        [D] => 0 
        [PF] => 2320 
        [PA] => 1974 
        [PD] => 346 
        [P] => 3 
       ) 

     ) 

    [2] => Array 
     (
      [id] => 1 
      [name] => abc 
      [overall] => Array 
       (
        [MP] => 2 
        [W] => 1 
        [L] => 1 
        [D] => 0 
        [PF] => 1620 
        [PA] => 1824 
        [PD] => -204 
        [P] => 3 
       ) 

     ) 

    [3] => Array 
     (
      [id] => 1 
      [name] => abc 
      [overall] => Array 
       (
        [MP] => 2 
        [W] => 2 
        [L] => 0 
        [D] => 0 
        [PF] => 2904 
        [PA] => 1932 
        [PD] => 972 
        [P] => 6 
       ) 

     ) 

) 

希望類似的這樣。

 
Array 
(
    [0] => Array 
     (
      [id] => 1 
      [name] => abc 
      [overall] => Array 
       (
        [MP] => 2 
        [W] => 2 
        [L] => 0 
        [D] => 0 
        [PF] => 2904 
        [PA] => 1932 
        [PD] => 972 
        [P] => 6 
       ) 

     ) 

    [1] => Array 
     (
      [id] => 1 
      [name] => abc 
      [overall] => Array 
       (
        [MP] => 2 
        [W] => 1 
        [L] => 1 
        [D] => 0 
        [PF] => 2320 
        [PA] => 1974 
        [PD] => 346 
        [P] => 3 
       ) 

     ) 

    [2] => Array 
     (
      [id] => 1 
      [name] => abc 
      [overall] => Array 
       (
        [MP] => 2 
        [W] => 1 
        [L] => 1 
        [D] => 0 
        [PF] => 1620 
        [PA] => 1824 
        [PD] => -204 
        [P] => 3 
       ) 

     ) 

    [3] => Array 
     (
      [id] => 1 
      [name] => abc 
      [overall] => Array 
       (
        [MP] => 2 
        [W] => 0 
        [L] => 2 
        [D] => 0 
        [PF] => 2200 
        [PA] => 2300 
        [PD] => -100 
        [P] => 0 
       ) 

     ) 

) 

我試過這樣。

 

    $memberArray1 = usort($memberArray, function($a, $b) { 
     return $b['overall']['P'] - $a['overall']['P']; 
    }); 
    $memberArray1 = usort($memberArray, function($a, $b) { 
     return $b['overall']['PD'] - $a['overall']['PD']; 
    }); 

但問題是,當我有點像寶潔這樣的,然後它工作,但之後當我有點像PD然後排序PD不喜歡P.

+0

發佈您的嘗試。你到目前爲止嘗試過什麼? –

回答

0

您可以使用uasortrsort一起爲

uasort($abc,function($a,$b){ 
    $c = $a['P'] - $b['P']; 
    $c .= $a['PD'] - $b['PD']; 
    $c .= $a['PF'] - $b['PF']; 
    $c .= $a['PA'] - $b['PA']; 
    return $c; 
}); 
array_reverse($abc); 
print_r($abc); 

Demo

+0

是有用的! 謝謝 –

+0

@NarendraSisodia您的功能可能會產生未定義的索引警告。另外,如果您在減法中反轉'$ a'和'$ b',則不需要'rsort'。 – Ugur

+2

在'uasort'之後做'rsort'應該使'uasort'無效。如果你想反轉方向,只需要'array_reverse',或者更好的方法,在比較函數中反轉$ a'和'$ b'。 – deceze