Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Sort a Multi-dimensional Array by Value

How can I sort this array by the value of the "order" key?

Even though the values are currently sequential, they will not always be.

Array (     [0] => Array         (             [hashtag] => a7e87329b5eab8578f4f1098a152d6f4             [title] => Flower             [order] => 3         )      [1] => Array         (             [hashtag] => b24ce0cd392a5b0b8dedc66c25213594             [title] => Free             [order] => 2         )      [2] => Array         (             [hashtag] => e7d31fc0602fb2ede144d18cdffd816b             [title] => Ready             [order] => 1         ) ) 
like image 907
stef Avatar asked Apr 23 '10 13:04

stef


People also ask

How do you sort a multidimensional array by value in Python?

To sort a two-dimensional list in Python use the sort() list method, which mutates the list, or the sorted() function, which does not. Set the key parameter for both types using a lambda function and return a tuple of the columns to sort according to the required sort order.

How do you sort a two dimensional array by column value?

To sort 2 dimensional array by column value with JavaScript, we can use the array sort method. const arr = [ [12, "AAA"], [12, "BBB"], [12, "CCC"], [28, "DDD"], [18, "CCC"], [12, "DDD"], [18, "CCC"], [28, "DDD"], [28, "DDD"], [58, "BBB"], [68, "BBB"], [78, "BBB"], ]; const sortedArr = arr.

Which function sort multiple array at once?

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.


2 Answers

Try a usort. If you are still on PHP 5.2 or earlier, you'll have to define a sorting function first:

function sortByOrder($a, $b) {     return $a['order'] - $b['order']; }  usort($myArray, 'sortByOrder'); 

Starting in PHP 5.3, you can use an anonymous function:

usort($myArray, function($a, $b) {     return $a['order'] - $b['order']; }); 

And finally with PHP 7 you can use the spaceship operator:

usort($myArray, function($a, $b) {     return $a['order'] <=> $b['order']; }); 

To extend this to multi-dimensional sorting, reference the second/third sorting elements if the first is zero - best explained below. You can also use this for sorting on sub-elements.

usort($myArray, function($a, $b) {     $retval = $a['order'] <=> $b['order'];     if ($retval == 0) {         $retval = $a['suborder'] <=> $b['suborder'];         if ($retval == 0) {             $retval = $a['details']['subsuborder'] <=> $b['details']['subsuborder'];         }     }     return $retval; }); 

If you need to retain key associations, use uasort() - see comparison of array sorting functions in the manual.

like image 99
Christian Studer Avatar answered Oct 26 '22 02:10

Christian Studer


function aasort (&$array, $key) {     $sorter = array();     $ret = array();     reset($array);     foreach ($array as $ii => $va) {         $sorter[$ii] = $va[$key];     }     asort($sorter);     foreach ($sorter as $ii => $va) {         $ret[$ii] = $array[$ii];     }     $array = $ret; }  aasort($your_array, "order"); 
like image 21
o0'. Avatar answered Oct 26 '22 01:10

o0'.