Hướng dẫn dùng array multisort trong PHP
Hướng dẫn cách sử dụng hàm array_multisort() về mảng trong lập trình PHP Show Tác dụng của hàm array_multisort()The Associative or string keys will be preserved, but numeric keys will be re-indexed. The following table summarizes the technical details of this function.
SyntaxThe basic syntax of the array_multisort(array1, array1_sort_order, array1_sort_flags, ...) The following example shows the
Tip: The first array is the main one to sort by; all items in the other arrays are reordered based on the sorted order of the first array (i.e., arrays are treated as columns of a table). If items in the first array compare as equal, the sort order is determined by the second array, and so on. In the above example after sorting, the first array will contain 2, 5, 7, 10, the second array will contain 4, 2, 3, 1. Notice that the entries in the second array corresponds to the entries in the first array. To better understand this, let's take a closer look at the following illustration.
ParametersThe
More ExamplesHere're some more examples showing how The following example shows how to sort a multi-dimensional array with this function.
Bài viết này đã giúp ích cho bạn?Bài viết mới(PHP 4, PHP 5, PHP 7, PHP 8) array_multisort — Sort multiple or multi-dimensional arrays Descriptionarray_multisort( Associative (string) keys will be maintained, but numeric keys will be re-indexed.
Parametersarray1 An array being sorted. array1_sort_order The order used to sort the previous array argument. Either This argument can be
swapped with array1_sort_flags Sort options for the previous array argument: Sorting type flags:
This argument can be swapped with rest More arrays, optionally followed by sort order and flags. Only elements corresponding to equivalent elements in previous arrays are compared. In other words, the sort is lexicographical. Return Values Returns ExamplesExample #1 Sorting multiple arrays
In this example, after sorting, the first array will contain 0, 10, 100, 100. The second array will contain 4, 1, 2, 3. The entries in the second array corresponding to the identical entries in the first array (100 and 100) were sorted as well. array(4) { [0]=> int(0) [1]=> int(10) [2]=> int(100) [3]=> int(100) } array(4) { [0]=> int(4) [1]=> int(1) [2]=> int(2) [3]=> int(3) } Example #2 Sorting multi-dimensional array
In this example, after sorting, the first array will transform to "10", 100, 100, 11, "a" (it was sorted as strings in ascending order). The second will contain 1, 3, "2", 2, 1 (sorted as numbers, in descending order). array(2) { [0]=> array(5) { [0]=> string(2) "10" [1]=> int(100) [2]=> int(100) [3]=> int(11) [4]=> string(1) "a" } [1]=> array(5) { [0]=> int(1) [1]=> int(3) [2]=> string(1) "2" [3]=> int(2) [4]=> int(1) } } Example #3 Sorting database results For this example, each element in the data array represents one row in a table. This type of dataset is typical of database records. Example data: volume | edition -------+-------- 67 | 2 86 | 1 85 | 6 98 | 2 86 | 6 67 | 7 The data as an array, called data. This would usually, for example, be obtained by looping with mysqli_fetch_assoc().
In this example, we will order by volume descending, edition ascending. We have an array of rows, but array_multisort() requires an array of columns, so we use the below code to obtain the columns, then perform the sorting.
The dataset is now sorted, and will look like this: volume | edition -------+-------- 98 | 2 86 | 1 86 | 6 85 | 6 67 | 2 67 | 7 Example #4 Case insensitive sorting Both To perform a case insensitive sort, force the sorting order to be determined by a lowercase copy of the original array.
The above example will output: Array ( [0] => Alpha [1] => atomic [2] => bank [3] => Beta ) See Also
jimpoz at jimpoz dot com ¶ 11 years ago
matt at bosc dot io ¶ 6 years ago
(array_column($array, 'key'), SORT_DESC, $array);?>
cagret at gmail dot com ¶ 13 years ago
= array( Robert C ¶ 8 years ago
nick ([AT]) nickyost ([DOT]) com ¶ 10 years ago
lingtalfi ¶ 1 year ago
$sorts as $field => $direction) { zequez at gmail dot com ¶ 12 years ago
frank at overdrunk dot net ¶ 13 years ago
RWC ¶ 16 years ago
Magento-User ¶ 8 years ago
alexander dot v at zend dot com ¶ 12 years ago
$sortArgs[] = &$valuesArray; scott - evolove - net - work it out ¶ 16 years ago
mech.cx ¶ 13 years ago
= array(1, 5, 2, 2); Patrick Adrichem ¶ 8 years ago
brettz9 throu gh yah ¶ 15 years ago
jcharpentier at darkmira dot fr ¶ 8 years ago
m dot michalczyk at gmail dot com ¶ 13 years ago
position{ 498936940 at qq dot com ¶ 10 months ago
Hayley Watson ¶ 4 years ago
ArkieGuy ¶ 4 years ago
qasastechnology at gmail dot com ¶ 3 years ago
faugeron dot loic at gmail dot com ¶ 6 years ago
= [ nospam at nospam dot com ¶ 6 years ago
matt at wennersten dot org ¶ 7 years ago
rnacken at gmail dot com ¶ 10 years ago
info at ichier dot de ¶ 11 years ago
thierryzo at hotmail dot fr ¶ 11 years ago
$myArray as $c=>$key) { Jaak Tamre ¶ 11 years ago
Zbigniew Heintze http://bigweb.pl ¶ 13 years ago
matt at idizinc dot com ¶ 13 years ago
isp dot php at spspft dot de ¶ 13 years ago
seiffs_at_centrum-dot-cz ¶ 14 years ago
Jon L. -- intel352 [AT] gmail [DOT] com ¶ 15 years ago
'Before sorting: KES http://kes.net.ua ¶ 16 years ago
php a-t-the-r-a-t-e chir.ag ¶ 16 years ago
$i = 0; $i < $n-1; $i++) meddle at dzygn.com ¶ 17 years ago
plugboard at web dot de ¶ 10 years ago
function sort_arr_of_obj($array, $sortby, $direction='asc') {$sortedArr = array(); joao at intrasystems dot com dot br ¶ 17 years ago
dwalter at seznam dot cz ¶ 2 years ago
walterquez at gmail dot com ¶ 4 years ago
|