PHP內建函數array_multisort 要求每個數組大小一樣
$areas是地區的二維數組,包含人數和次數,現在要按這2種數進行降序排序
foreach($areaArray as &$areas) { $times = $numbers = array(); foreach($areas as $province => $v) { $times[$province] = $v['times']; $numbers[$province] = $v['numbers']; } array_multisort($times, SORT_DESC, $numbers, SORT_DESC, $areas); }
例如有多個為數組:對二維數組中的age 項排序。
需要用到PHP的內建函數 array_multisort(),可以看手冊。自訂函數:
$arr = array( ‘d' => array(‘id' => 5, ‘name' => 1, ‘age' => 7), ‘b' => array(‘id' => 2,'name' => 3,'age' => 4), ‘a' => array(‘id' => 8,'name' => 10,'age' => 5), ‘c' => array(‘id' => 1,'name' => 2,'age' => 2) );
更多PHP多維數組的排序問題 根據二維數組中某項排序相關文章請關注PHP中文網!