Paulund

Sort Multi-Dimensional Array By Value In PHP

The following code snippet will help to sort multi-dimensional arrays by a value of one of the elements. If you look at the code below we have an array populated with 3 other arrays, each of them has an ID element, a title element and an order element. At the moment these are simply in order of how they were added to the array but because there is an order element identifying what position they should be in we need to re-sort these in the correct order.


Array
(
    [item-1] => Array
        (
            [id] => 1
            [title] => Item 1
            [order] => 3
        )

    [item-2] => Array
        (
            [id] => 2
            [title] => Item 2
            [order] => 2
        )

    [item-3] => Array
        (
            [id] => 3
            [title] => Item 3
            [order] => 1
        )
)

We do have a couple of options when it comes to sorting arrays, PHP comes with loads of functions that are designed to make sorting arrays easier. Array Sorting Some of the functions you can use are: - array_multisort()

  • asort()
  • arsort()
  • krsort()
  • ksort()
  • natcasesort()
  • natsort()
  • rsort()
  • shuffle()
  • sort()
  • uasort()
  • uksort()
  • usort()

The difference between these functions is that some will sort by value, sort by keys, maintain the keys, replace the keys, user defined ordering, alphabetically order, there are lots of different ways you would want to sort an array so you need to make sure you are going to use the correct one. For the above example we are going to use the function usort() as it allows the user to define what values you want to use to sort on, as we need to sort on the order element this is exactly what we want.

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

The usort() function is used by passing in the array and callback function that you want to use to compare the array element, PHP will then pass into this function the fields of the array as two parameters which you can then compare and values you define and will know which way to sort these by how you return the values from the function. Below is how the above usort function will be returned.

Array
(
    [0] => Array
        (
            [id] => 3
            [title] => Item 3
            [order] => 1
        )

    [1] => Array
        (
            [id] => 2
            [title] => Item 2
            [order] => 2
        )

    [2] => Array
        (
            [id] => 1
            [title] => Item 1
            [order] => 3
        )
)

As you can see the array elements are now in the correct order by using the value inside the order element. But the problem with using usort is that it will remove the existing keys from your array and use the standard index keys. The solution to this problem is to use a different sorting function called uasort() which will maintain your defined array keys.

uasort($array, 'sort_by_order ');
function sort_by_order ($a, $b)
{
    return $a['order'] - $b['order'];
}

Used exactly the same way as usort(), but looking at the output below you will see that the keys are now maintained to how they were defined.


Array
(
    [item-3] => Array
        (
            [id] => 3
            [title] => Item 3
            [order] => 1
        )

    [item-2] => Array
        (
            [id] => 2
            [title] => Item 2
            [order] => 2
        )

    [item-1] => Array
        (
            [id] => 1
            [title] => Item 1
            [order] => 3
        )
)