編程過程中,如果有需要對某個數組根據某個字段來排序 ,可以用array_multisort函數,很實用。
實例:
$data = array(array("id" => 1, "name" => "Boney M", "rating" => 3), array("id" => 2, "name" => "Take That", "rating" => 1), array("id" => 3, "name" => "The Killers", "rating" => 4), array("id" => 4, "name" => "Lusain", "rating" => 3), ); foreach ($data as $key => $value) { $name[$key] = $value['name']; $rating[$key] = $value['rating']; } print_r($data); array_multisort($rating, $name,SORT_DESC, $data); print_r($data);
這裡表示先按$rating的倒序排 如果一樣 再按$name 的倒序排 SORT_DESC這個參數表示排序的形式····