Multi Dimensional Array Sorting in Php

By | May 17, 2018

There are some inbuilt php functions are available to sort an array in which order you want like sort(), ksort(), rsort(), usort() etc. But these methods sort only one dimensional array. If you want multi dimensional array sorting you will have to write some code to handle it. Here, in this article i will tell you a simple way of multi dimensional array sorting in php.

There can be many methods to do this task. You can use any method to get the proper result you want.


uasort($myArray, function($a, $b) {
    return strcmp($a['title'], $b['title']);

In this method i used inbuilt php function uasort, passed an array $myArray and a closer function as second parameter.


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

Same function used as in method 1. But as you can see that body of closer function is different and both will produce same result.


function aasort (&$array, $key) {
    foreach ($array as $ii => $va) {
    foreach ($sorter as $ii => $va) {


In this third method i have created a function aasort.  This method has two parameters. First one is array and second one is key of the array from which you want to sort.


function sortArrayBycolumn(&$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($sortArrayBycolumn, 'order');

In this function there are three parameters. But third parameter is optional which is sorting order.

You can use any of these methods to sort a multi dimensional array.  If there is any query in your mind please comment here. I will try to solve.