How does PHP implement two-dimensional array sorting?
Two-dimensional arrays are often encountered in PHP development, but his sorting is not as easy as a one-dimensional array with built-in functions, (one-dimensional array sorting can refer to the other article "PHP Array Sorting function detailed summary "). The ordering of two-dimensional arrays requires that we write our own function processing, here Uncletoo for you to share a PHP two-dimensional array sorting function:
Code:
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;}
?
The three parameter descriptions of the function:
$arr: the array to sort
$keys: Specifies which key value to sort by
$type: Sort by, ascending or descending, default to ascending ?
This PHP function can be implemented to sort a two-dimensional array according to the specified key value and return the sorted array.
Invocation Example:
?
$newArray = Array_sort ($array, ' price ');
?
Original source:http://www.uncletoo.com/html/base/657.html