This article analyzes the usage of PHP array function array_multisort() with examples. Share it with everyone for your reference, the details are as follows:
Sometimes we need to sort the value of a key in a two-dimensional array. This issue is discussed here. We can use the array_multisort() function. The array_multisort() function sorts multiple arrays or multidimensional arrays.
The array in the parameter is treated as a table column and sorted by row - this is similar to the functionality of SQL's ORDER BY clause. The first array is the main array to be sorted. If the rows (values) in the array compare to be the same, they will be sorted according to the size of the corresponding value in the next input array, and so on.
The first parameter is an array, and each subsequent parameter may be an array or one of the following sort order flags (the sort flag is used to change the default sort order):
SORT_ASC - Default, sort in ascending order. (A-Z)
SORT_DESC - Sort in descending order. (Z-A)
The type of sort can then be specified:
SORT_REGULAR - Default. Arrange each item in regular order.
SORT_NUMERIC - Sort each item in numerical order.
SORT_STRING - Sort each item in alphabetical order.
Syntax: array_multisort(array1,sorting order,sorting type,array2,array3...)
array1: required. Specifies the input array.
sorting order: optional. Specify the order of sorting. Possible values are SORT_ASC and SORT_DESC.
sorting type: optional. Specifies the sorting type. Possible values are SORT_REGULAR, SORT_NUMERIC and SORT_STRING.
array2: optional. Specifies the input array.
array3: optional. Specifies the input array.
String key names will be preserved, but numeric keys will be re-indexed, starting at 0 and increasing by 1. The sort order and sort type can be set after each array. If not set, each array parameter will use its default value.
Here is an example:
<?php $arr = ''; echo '二维数组如下:'.'<br / >'; for($i=0; $i<=5; $i++) { $arr[$i]['val'] = mt_rand(1, 100); $arr[$i]['num'] = mt_rand(1, 100); } echo '<pre class="brush:php;toolbar:false">'; print_r($arr); echo ''; echo '从二维数组中抽出键为val,单独成另一个数组:'.'
'; print_r($vals); echo ''; echo '对其进行排序:'.'
'; print_r($vals); echo ''; ?>
Run result:
二维数组如下: Array ( [0] => Array ( [val] => 46 [num] => 49 ) [1] => Array ( [val] => 8 [num] => 24 ) [2] => Array ( [val] => 37 [num] => 3 ) [3] => Array ( [val] => 32 [num] => 35 ) [4] => Array ( [val] => 19 [num] => 38 ) [5] => Array ( [val] => 30 [num] => 37 ) ) 从二维数组中抽出键为val,单独成另一个数组: Array ( [0] => 46 [1] => 8 [2] => 37 [3] => 32 [4] => 19 [5] => 30 ) 对其进行排序: Array ( [0] => 8 [1] => 19 [2] => 30 [3] => 32 [4] => 37 [5] => 46 )
In this way we will get a two-dimensional array sorted by val in ascending order.
Readers who are interested in more PHP-related content can check out the special topics on this site: "Summary of PHP operations and operator usage", "Summary of PHP network programming skills", " Introductory tutorial on PHP basic syntax", "Summary of PHP office document operation skills (including word, excel, access, ppt)", "Summary of PHP date and time usage》, "php object-oriented programming introductory tutorial", "php string (string) usage summary", "php mysql database operation introductory tutorial" And "Summary of common database operation skills in PHP"
I hope this article will be helpful to everyone in PHP programming.