我有以下数组:
$name_arr = array('raj','raj','ganesh','rahul','ganesh','mayur','raj','rahul');
我想这样排序:
$final_arr = array('raj','raj','raj','ganesh','ganesh','rahul','rahul','mayur');
我怎样才能实现它?
解决方法:
使用array_count_values和arsort的简单方法: –
$array = array_count_values($name_arr); //get all occurrences of each values
arsort($array);
print_r($array);//print occurrences array
$final_array = array();
foreach($array as $key=>$val){ // iterate over occurrences array
for($i=0;$i<$val;$i++){ //apply loop based on occurrences number
$final_array[] = $key; // assign same name to the final array
}
}
print_r($final_array); // print final array