How to Sort a Multi-dimensional Array by Value How to Sort a Multi-dimensional Array by Value arrays arrays

How to Sort a Multi-dimensional Array by Value


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.


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");


I use this function:

function array_sort_by_column(&$arr, $col, $dir = SORT_ASC) {    $sort_col = array();    foreach ($arr as $key => $row) {        $sort_col[$key] = $row[$col];    }    array_multisort($sort_col, $dir, $arr);}array_sort_by_column($array, 'order');

EditThis answer is at least ten years old, and there are likely better solutions now, but I am adding some extra info as requested in a couple of comments.

It works because array_multisort() can sort multiple arrays. Example input:

Array(    [0] => Array        (            [hashtag] => a7e87329b5eab8578f4f1098a152d6f4            [title] => Flower            [order] => 3        )    [1] => Array        (            [hashtag] => b24ce0cd392a5b0b8dedc66c25213594            [title] => Free            [order] => 2        )

First $sort_col is made which is an two dimensional array with the values being what we want to sort by and the keys matching the input array. For example for this input, choosing key $sort_col "order" we get:

Array(    [0] => 3,    [1] => 2)

array_multisort() then sorts that array (resulting in key order 1, 0) but this is only the two dimensional array. So the original input array is also passed as the $rest argument. As the keys match it will be sorted so its keys are also in the same order, giving the desired result.

Note:

  • it is passed by reference so that the supplied array is modified in place.
  • array_multisort() can sort multiple additional array like this, not just one