Two-dimensional arrays are often encountered in PHP development, but his ranking is not as convenient as a one-dimensional array with built-in functions, the order of two-dimensional arrays needs our own Write function processing, here Uncletoo to share a PHP two-dimensional array sorting function:
Code:
Copy Code code as follows:
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 parameters of the function are described:
$arr: Array to sort
$keys: Specify which key value to sort by
$type: Sort, ascending or descending, default to Ascending
This PHP function enables you to sort a two-dimensional array based on the specified key value and return the sorted array.
Call Example:
Copy Code code as follows:
$newArray = Array_sort ($array, ' price ');