Two-dimensional arrays are often used in PHP development, but their sorting is not as convenient as a one-dimensional array with built-in functions. we need to write a function to sort two-dimensional arrays, here, UncleToo will share with you a PHP two-dimensional array sorting function:
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;
}
Three parameters of the function are described as follows:
$ Arr: array to be sorted
$ Keys: specifies the key value to sort.
$ Type: sorting method, in ascending or descending order. the default value is ascending.
This PHP function can sort a two-dimensional array by the specified key value and return the sorted array.
Call example:
$ NewArray = array_sort ($ array, 'price ');