我想按值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.
發佈您的嘗試。你到目前爲止嘗試過什麼? –