Php sort multidimensional array by value
This solution is for usort() with an easy-to-remember notation for multidimensional sorting. The spaceship operator <=> is used, which is available from PHP 7. Show
Examples:
First task: Order By rank asc, surname asc
Second task: Order By rank desc, surname asc, firstmame asc
Third task: Order By rank desc, birthdate asc The date cannot be sorted in this notation. It is converted with strtotime. (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 ¶ 11 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 ¶ 16 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 ¶ 11 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
How do I sort a multi dimensional array in PHP?The array_multisort() function returns a sorted array. You can assign one or more arrays. The function sorts the first array, and the other arrays follow, then, if two or more values are the same, it sorts the next array, and so on.
How do you sort an array of associative arrays by the value of a given key in PHP?The arsort() function sorts an associative array in descending order, according to the value. Tip: Use the asort() function to sort an associative array in ascending order, according to the value. Tip: Use the krsort() function to sort an associative array in descending order, according to the key.
Which function sorts multiple array at once?array_multisort() can be used to sort several arrays at once, or a multi-dimensional array by one or more dimensions. Associative (string) keys will be maintained, but numeric keys will be re-indexed.
How do you sort a 2D array?Make the 2D array into a separate simple (1D) array (STEP 1). Then use the Arrays. sort() method to sort the simple array (STEP 2). Then set each space of the 2D array to be the number of columns across (X-coordinate where the space will be changed) multiplied by the number of spaces per row in the 2D array.
|