How to Sort a Multidimensional Array in PHP



PHP







In this article, we show how to sort a multidimensional array in PHP.

We sort the multidimensional array using the array_multisort() function.

The array_multisort() function sorts the first column element of an array in order.

For example, if we have a multidimensional array of books and the first column is the titles of the books, it will sort the multidimensional array in order of the titles alphabetically.

The ar

Below we create a multidimensional array of books, that have title, publication year, and author columns.

We then use the array_multisort() function to sort the elements of the array in order.

Since the first column of the arrays is the title column, this code sorts the titles of the books in alphabetical order.

This is all shown below.



So running the code above gives use the following output below.

Actual PHP Output


Array
(
    [0] => Array
        (
            [title] => Best of Both Seas
            [publicationyear] => 2012
            [author] => Seagell, Bill
        )

    [1] => Array
        (
            [title] => Going Under Siege
            [publicationyear] => 2009
            [author] => Mandoe, Gregory
        )

    [2] => Array
        (
            [title] => Moving to Worlds Beyond
            [publicationyear] => 2008
            [author] => Marks, Ryan
        )

)
1



So you can see above how the titles of the books are shown in alphabetical order.

The array_multisort() functions works very well for this purpose.

But what if you want to sort the books by the publication year or by the author's name.

This can be done but unfortunately not without a bit of work. In order for this to happen with the array_multisort() function, you would have to change the order of the columns. The array_multisort() function only sorts the first column of an array. So however you want to sort an array (by which column), you would have to put that column first.

So if you wanted to sort the books by publication year, for instance, you would have to put that column first in the multidimensional array.

So if I put the publication year column first in each of the arrays within the multidimensional array and ran the code, I get the following output shown below.

Actual PHP Output


Array
(
    [0] => Array
        (
            [publicationyear] => 2008
            [title] => Moving to Worlds Beyond
            [author] => Marks, Ryan
        )

    [1] => Array
        (
            [publicationyear] => 2009
            [title] => Going Under Siege
            [author] => Mandoe, Gregory
        )

    [2] => Array
        (
            [publicationyear] => 2012
            [title] => Best of Both Seas
            [author] => Seagell, Bill
        )

)
1



So now with the publication year data first in the array, the array can now be sorted according to the publication year of the books.

There are other ways of sorting multidimensional arrays in PHP. This, though, is probably the simplest way.

If you only need to sort the multidimensional array according to one set of information (for example, the title of the books), it works very well and is extremely simple code. However, if you are running a very dynamic application, where you need to sort multidimensional ways in multiple ways, then this way probably isn't the best. This is because you would have to structure the multidimensional array differently each time. You could create multiple multidimensional arrays each structured differently but it's not the most efficient. In that case, you would need to create custom code that sort multidimensional arrays instead of using the built-in array_multisort() function.

But for simplistic purposes, the array_multisort() function works very well to sort multidimensional arrays.


Related Resources

How to Create a Multidimensional Array in PHP

How to Access the Elements of a Multidimensional Array in PHP

How to Loop Through a Multidimensional Array in PHP

HTML Comment Box is loading comments...