How to Sort a Multi-dimensional Array by Value in PHP?

Problem:

You have a multi-dimensional array, for example the following one. And, as we can see the values are not sorted in the array. So, you like to sort the values according to the developers’ names. Or, suppose you’ve got a multi-dimensional array and you’re not sure whether the values inside it are sorted.

<?php
$developers = array(
                    array("name" => "Preston J. Hansen",
                          "age" => 26,
                          "specialty" => "PHP"),
                    array("name" => "William M. Sterling",
                          "age" => 30,
                          "specialty" => "C#"),
                    array("name" => "Caroline M. Byrge",
                          "age" => 24,
                          "specialty" => "Android"),
                    array("name" => "Harriet R. Roberts",
                          "age" => 23,
                          "specialty" => "iPhone")
                  );
?>

Solution:

Use usort() function. It will sort the values according to a condition. For example, in your above array, your condition would be to sort the array according to the developers’ names.

The code:

<?php
function cmp($a, $b){
    if ($a["name"] == $b["name"]) {
        return 0;
    }
    return ($a["name"] < $b["name"]) ? -1 : 1;
}

$developers = array(
                    array("name" => "Preston J. Hansen",
                          "age" => 26,
                          "specialty" => "PHP"),
                    array("name" => "William M. Sterling",
                          "age" => 30,
                          "specialty" => "C#"),
                    array("name" => "Caroline M. Byrge",
                          "age" => 24,
                          "specialty" => "Android"),
                    array("name" => "Harriet R. Roberts",
                          "age" => 23,
                          "specialty" => "iPhone")
                    );
usort($developers, "cmp");

foreach($developers as $key => $value)
    echo $value["name"]. " - " .$value["age"]. " - " .$value["specialty"] . "<br />";
?>

[wpdm_file id=27]
Output:
Caroline M. Byrge – 24 – Android
Harriet R. Roberts – 23 – iPhone
Preston J. Hansen – 26 – PHP
William M. Sterling – 30 – C#

How code works:
usort() function sort values inside an array using a user-defined comparison function. Here, our array is $developer[] and the comparison function is cmp(). We want to sort the array according to the developer names.

Line 23 Here, usort() function is told to sort the array $developer according to the condition defined in the “cmp” function. The usort() function takes 2 items (here, items are internal arrays) at a time from the beginning of the array $developer serially and sort them according to the value the “cmp” function returns. Depending on the three different values the “cmp” function returns, the usort() function sorts the values according to the following rules-
‘If the function returns -1, it moves the second item down.
‘If the function returns 1, it moves the second item up.
‘If the function returns 0, it keeps the items in the same positions.
Let’s see how does the “cmp” function work.
Line 2 Here, $a and $b represent first two arrays from the multi-dimensional array.
Line 3 As you want to sort the array by the developers’ names, so we mention it here. If both names are same, then, no need to sort. So we returns 0 in line 4
Line 6 If the name in the first array is smaller than the name in the second array, then return -1 else return 1.
Line 29-30 We just print the sorted array.


The code (shorter version)
Using anonymous function(which was introduced from PHP 5.3.0), we can shorter the above code.

<?php
$developers = array(
                    array("name" => "Preston J. Hansen",
                          "age" => 26,
                          "specialty" => "PHP"),
                    array("name" => "William M. Sterling",
                          "age" => 30,
                          "specialty" => "C#"),
                    array("name" => "Caroline M. Byrge",
                          "age" => 24,
                          "specialty" => "Android"),
                    array("name" => "Harriet R. Roberts",
                          "age" => 23,
                          "specialty" => "iPhone")
                    );
usort($developers, function ($a, $b){
    if ($a["name"] == $b["name"]) {
        return 0;
    }
    return ($a["name"] < $b["name"]) ? -1 : 1;
}
);

foreach($developers as $key => $value)
    echo $value["name"]. " - " .$value["age"]. " - " .$value["specialty"] . "<br />";
?>

[wpdm_file id=28]