Php sort multidimensional array by value alphabetically
Here is your answer and it works 100%, I've tested it. Show (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 ¶ 17 years ago
Magento-User ¶ 8 years ago
faugeron dot loic at gmail dot com ¶ 6 years ago
= [ 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
alexander dot v at zend dot com ¶ 12 years ago
$sortArgs[] = &$valuesArray; m dot michalczyk at gmail dot com ¶ 13 years ago
position{ 498936940 at qq dot com ¶ 11 months ago
dwalter at seznam dot cz ¶ 2 years ago
Hayley Watson ¶ 4 years ago
ArkieGuy ¶ 4 years ago
qasastechnology at gmail dot com ¶ 3 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++) joao at intrasystems dot com dot br ¶ 17 years ago
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(); walterquez at gmail dot com ¶ 4 years ago
How do I sort a multi dimensional array in PHP?Sorting a multidimensional array by element containing date. Use the usort() function to sort the array. The usort() function is PHP builtin function that sorts a given array using user-defined comparison function. This function assigns new integral keys starting from zero to array elements.
How do you put an array in alphabetical order in PHP?PHP - Sort Functions For Arrays
sort() - sort arrays in ascending order. rsort() - sort arrays in descending order. asort() - sort associative arrays in ascending order, according to the value. ksort() - sort associative arrays in ascending order, according to the key.
Can we sort multidimensional array?In a 2D array, a cell has two indexes one is its row number, and the other is its column number. Sorting is a technique for arranging elements in a 2D array in a specific order. The 2D array can be sorted in either ascending or descending order.
How do you sort a multidimensional list?Use sorted() with a lambda function to sort a multidimensional list by column. Call sorted(iterable, key=None) with key set to a lambda function of syntax lambda x: x[i] to sort a multidimensional list iterable by the i th element in each inner list x .
|