-
- functionarray_sort($arr,$keys,$type='asc'){
- $keysvalue= $new_array= array();
- foreach($arras$k=>$v) {
- $keysvalue[$k] = $v[$keys];
- }
- if($type== 'asc'){
- asort($keysvalue);
- }else{
- arsort($keysvalue);
- }
- reset($keysvalue);
- foreach($keysvalueas$k=>$v){
- $new_array[$k] = $arr[$k];
- }
- return$new_array;
- }
Copy code Description of the three parameters of the
function:
$arr: the array to sort
$keys: Specify which key value to sort by
$type: sorting method, ascending or descending order, the default is ascending order
This PHP function sorts a two-dimensional array according to the specified key value and returns the sorted array.
Example:
-
- $newArray= array_sort($array,'price');
Copy code
|