There are two ways to sort a two-dimensional array according to a field. One is to write your own code through sort, and the other is to use array directly_ Multisort sort function
I Handwritten arraysort
PHP's one-dimensional array sorting function:
sort sorts the values of the array in ascending order (rsort descending order) without retaining the original keys
ksort arranges the keys of the array in ascending order (krsort descending order) and retains the key value relationship
asort arranges the values of the array in ascending order (arsort in descending order) and retains the key value relationship
Methods: take out the values to be sorted to form a value array (a one-dimensional array), sort the values as required (maintain the key value relationship), traverse the value array, and assign values to the result array according to the key.
function arraySort($array,$keys,$sort='asc') { $newArr = $valArr = array(); foreach ($array as $key=>$value) { $valArr[$key] = $value[$keys]; } ($sort == 'asc') ? asort($valArr) : arsort($valArr); reset($valArr); foreach($valArr as $key=>$value) { $newArr[$key] = $array[$key]; } return $newArr; }
II Through array_ Sort array fields with multisort
This is also the main point of this article.
Array on official website_ Explanation of multisort function: sort multiple arrays or multidimensional arrays
bool array_multisort ( array $ar1 [, mixed $arg [, mixed $... [, array $... ]]] )
Returns TRUE on success or FALSE on failure
array_multisort() can be used to sort multiple arrays at once, or to sort multidimensional arrays according to one or more dimensions. The associated (string) key name remains unchanged, but the numeric key name is re indexed.
The input array is treated as a column of a table and sorted by rows -- similar to the function of the ORDER BY clause of SQL. The first array is the main array to sort. If the rows (values) in the array are the same, they will be sorted according to the size of the corresponding value in the next input array, and so on.
The parameter structure of this function is somewhat unusual, but it is very flexible. The first argument must be an array. Each of the following parameters can be an array or a sort flag listed below.
Sort order flag:
- SORT_ASC - sort in ascending order
- SORT_DESC - sort in descending order
Sort type flag:
- SORT_REGULAR - compare items in the usual way
- SORT_NUMERIC - compare items by value
- SORT_STRING - compare items by string
You cannot specify two similar sort flags after each array. The sort flag specified after each array is valid only for that array - previously the default value was SORT_ASC and SORT_REGULAR.
Example 1:
$ar1 = array("10", 101, 100, 6); $ar2 = array(1, 3, 6, 5); array_multisort($ar1, $ar2); print_r($ar1); print_r($ar2);
The order of items in the second array is exactly the same as that of the corresponding items in the first array.
Array ( [0] => 6 [1] => 10 [2] => 100 [3] => 101 ) Array ( [0] => 5 [1] => 1 [2] => 6 [3] => 3 )
Example 2:
$ar = array( array("b10", 'c11', 101, 100, "a"), array(1, 2, "2", 9, 5) ); array_multisort($ar[0], SORT_ASC, SORT_STRING, $ar[1], SORT_NUMERIC, SORT_DESC); print_r($ar);
After sorting, the first array will contain "b10", "c11", 101100, "a" (sorted as string rising), and the second array will contain 1, 2, "2", 9, 5 (sorted as value falling).
Array ( [0] => Array ( [0] => 100 [1] => 101 [2] => a [3] => b10 [4] => c11 ) [1] => Array ( [0] => 9 [1] => 2 [2] => 5 [3] => 1 [4] => 2 ) )
Example 3: (the result we want)
$data[] = array('customer_name' => 'petty thief', 'money' => 12, 'distance' => 2, 'address' => 'Chang'an Street C Square'); $data[] = array('customer_name' => 'Wang Xiao', 'money' => 30, 'distance' => 10, 'address' => '30 North Street'); $data[] = array('customer_name' => 'Zhao Xiaoya', 'money' => 89, 'distance' => 6, 'address' => 'Jiefang Road Hengji building A Seat'); $data[] = array('customer_name' => 'Xiaoyue', 'money' => 150, 'distance' => 5, 'address' => '400m east of overpass'); $data[] = array('customer_name' => 'Li Liangliang', 'money' => 45, 'distance' => 26, 'address' => 'Lane 198, Tianshan West Road'); $data[] = array('customer_name' => 'Dong Juan', 'money' => 67, 'distance' => 17, 'address' => '2 Xinda South Road'); // Get a list of columns foreach ($data as $key => $row) { $distance[$key] = $row['distance']; $money[$key] = $row['money']; } array_multisort($distance, SORT_DESC, $data); print_r($data);
$data contains an array of rows, but array_multisort() requires an array of columns, so use foreach to get the columns and sort them.
All, we will array_multisort() encapsulates:
/** * A two-dimensional array is sorted by a field * @param array $array Array to sort * @param string $keys Key fields to sort * @param string $sort Sort type SORT_ASC SORT_DESC * @return array Sorted array */ function arraySort($array, $keys, $sort = SORT_DESC) { $keysValue = []; foreach ($array as $k => $v) { $keysValue[$k] = $v[$keys]; } array_multisort($keysValue, $sort, $array); return $array; }
Continue to use the $data array in the above example
# Sort by distance in descending order $a = arraySort($data, 'distance', SORT_DESC); print_r($a); # Sort by money in ascending order $b = arraySort($data, 'money', SORT_ASC); print_r($b);
effect:
Array ( [0] => Array ( [customer_name] => Li Liangliang [money] => 45 [distance] => 26 [address] => Lane 198, Tianshan West Road ) [1] => Array ( [customer_name] => Dong Juan [money] => 67 [distance] => 17 [address] => 2 Xinda South Road ) [2] => Array ( [customer_name] => Wang Xiao [money] => 30 [distance] => 10 [address] => 30 North Street ) [3] => Array ( [customer_name] => Zhao Xiaoya [money] => 89 [distance] => 6 [address] => Jiefang Road Hengji building A Seat ) [4] => Array ( [customer_name] => Xiaoyue [money] => 150 [distance] => 5 [address] => 400m east of overpass ) [5] => Array ( [customer_name] => petty thief [money] => 12 [distance] => 2 [address] => Chang'an Street C Square ) )
Array ( [0] => Array ( [customer_name] => Li Liangliang [money] => 45 [distance] => 26 [address] => Lane 198, Tianshan West Road ) [1] => Array ( [customer_name] => Dong Juan [money] => 67 [distance] => 17 [address] => 2 Xinda South Road ) [2] => Array ( [customer_name] => Wang Xiao [money] => 30 [distance] => 10 [address] => 30 North Street ) [3] => Array ( [customer_name] => Zhao Xiaoya [money] => 89 [distance] => 6 [address] => Jiefang Road Hengji building A Seat ) [4] => Array ( [customer_name] => Xiaoyue [money] => 150 [distance] => 5 [address] => 400m east of overpass ) [5] => Array ( [customer_name] => petty thief [money] => 12 [distance] => 2 [address] => Chang'an Street C Square ) ) Array ( [0] => Array ( [customer_name] => petty thief [money] => 12 [distance] => 2 [address] => Chang'an Street C Square ) [1] => Array ( [customer_name] => Wang Xiao [money] => 30 [distance] => 10 [address] => 30 North Street ) [2] => Array ( [customer_name] => Li Liangliang [money] => 45 [distance] => 26 [address] => Lane 198, Tianshan West Road ) [3] => Array ( [customer_name] => Dong Juan [money] => 67 [distance] => 17 [address] => 2 Xinda South Road ) [4] => Array ( [customer_name] => Zhao Xiaoya [money] => 89 [distance] => 6 [address] => Jiefang Road Hengji building A Seat ) [5] => Array ( [customer_name] => Xiaoyue [money] => 150 [distance] => 5 [address] => 400m east of overpass ) )