- /*
- $array_name: the incoming array;
- $row_id: the items in the array that you want to sort;
- $order_type: the sorting method, ASC or DESC;
- */
- function array_sort($ array_name,$row_id,$order_type){
- $array_temp=array();
- foreach($array_name as $key=>$value){//Loop one level;
- $array_temp[$key]=$value[$ row_id];//Create a new one-dimensional array, the index value uses the index value of the two-dimensional array; the value is the value of the item to be compared in the two-dimensional array;
- }
- if ($order_type==="ASC"){
- asort($array_temp);
- }else{
- arsort($array_temp);
- }
- $result_array=array();
- foreach($array_temp as $key=>$value){//Filtered array Traverse;
- $result_array[]=$array_name[$key];//Create a new result array, change the key value order of the original array and assign it to the result array (the original array remains unchanged);
- }
- return $result_array ;
- }
- //The following is the test
- $arr=array(array('num'=>7,'value'=>4),
- array('num'=>3,'value'=> ;45),
- array('num'=>38,'value'=>27));
- $sortarr=array_sort($arr,'value','ASC');
- print_r($sortarr);
- ?>
Copy code
|