Two-dimensional arrays are often used in PHP development, but their sorting is not as convenient as using built-in functions as one-dimensional arrays, the following describes in detail the two-dimensional arrays that are frequently encountered in PHP development, but their sorting is not as convenient as the one-dimensional array using built-in functions, to sort two-dimensional arrays, we need to write our own function. here, UncleToo will share with you a function for sorting two-dimensional arrays in PHP:
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 ');
For more information about the function of sorting two-dimensional PHP arrays, see the Chinese PHP website!