How to sort a multidimensional array?

php programming

Let’s use the array “goods” of the following form:

<?php

$goods[65] = array("price" => 200, "manufacture" => "Zelina");
$goods[45] = array("price" => 400, "manufacture" => "Devyatkin");
$goods[78] = array("price" => 800, "manufacture" => "Agrarnik");
$goods[89] = array("price" => 790, "manufacture" => "Belyi Orel");

?>


It is necessary to sort the given array by key. To this end, we are to use the finction “ksort”:

<?php

ksort($goods);
print_r($goods);

?>

As a result, we’ll have the key sorted array:

Array
(
    [45] => Array
        (
            [price] => 400
            [manufacture] => Devyatkin
        )

    [65] => Array
        (
            [price] => 200
            [manufacture] => Zelina
        )

    [78] => Array
        (
            [price] => 800
            [manufacture] => Agrarnik
        )

    [89] => Array
        (
            [price] => 790
            [manufacture] => Belyi Orel
        )

)

As we see the keys are in ascending order from 45 to 89. Now, it is necessary to sort the array by value of the key «price». For this, we are to use the function “uasort” and to write the user-defined function “sort_p” for it:

<?php

function sort_p($a, $b)
{
return strcmp($a["price"], $b["price"]);
}
uasort($goods, "sort_p");
print_r($goods);

?>

As a result, we’ll have the array sorted by the key “price”:

Array
(
    [65] => Array
        (
            [price] => 200
            [manufacture] => Zelina
        )

    [45] => Array
        (
            [price] => 400
            [manufacture] => Devyatkin
        )

    [89] => Array
        (
            [price] => 790
            [manufacture] => Belyi Orel
        )

    [78] => Array
        (
            [price] => 800
            [manufacture] => Agrarnik
        )

)

As we see the values of the key “price” are in ascending order from 200 to 800. To change the values of the key “price” to decreasing order, we are to swap the parameters of the function “strcmp” in the user-defined function “sort_p”:

<?php

function sort_p($a, $b)
{
return strcmp($b["price"], $a["price"]);
}
uasort($goods, "sort_p");
print_r($goods);

?>


We’ll get the following result:

Array
(
    [78] => Array
        (
            [price] => 800
            [manufacture] => Agrarnik
        )

    [89] => Array
        (
            [price] => 790
            [manufacture] => Belyi Orel
        )

    [45] => Array
        (
            [price] => 400
            [manufacture] => Devyatkin
        )

    [65] => Array
        (
            [price] => 200
            [manufacture] => Zelina
        )

)

As we can see, now the values of the key “price” are in decreasing order from 800 to 200. Now we need to sort the array by the values of two keys “manufacture” and “price”. For this, we need to write the user-defined function “sort_pm”:

<?php

function sort_pm($a, $b)
{
  $r1 = strcmp($a["manufacture"], $b["manufacture"]);
  return ($r1 == 0) ? strcmp($a["price"], $b["price"]) : $r1;
}
uasort($goods, "sort_pm");
print_r($goods);

?>

Now the array is sorted by the values of two keys in ascending order, the “manufacture” key is a priority.

Array
(
    [78] => Array
        (
            [price] => 800
            [manufacture] => Agrarnik
        )

    [89] => Array
        (
            [price] => 790
            [manufacture] => Belyi Orel
        )

    [45] => Array
        (
            [price] => 400
            [manufacture] => Devyatkin
        )

    [65] => Array
        (
            [price] => 200
            [manufacture] => Zelina
        )

)

If the values of the “price” key is of higher priority for you, swap the parameters of the functions “strcmp” and write the function “sort_pm” in the following way:

<?php

function sort_pm($a, $b)
{
  $r1 = strcmp($a["price"], $b["price"]);
  return ($r1 == 0) ? strcmp($a["manufacture"], $b["manufacture"]) : $r1;
}
uasort($goods, "sort_pm");
print_r($goods);

?>

That is parameters of the functions “strcmp” were changed. Now the values of the “price” key will be of higher priority; it means that the sorting will first of all be done by them, then the values of the “manufacture” key will be sorted. Let’s illustrate by an example:

<?php

$goods[65] = array("price" => 200, "manufacture" => "Zelina");
$goods[45] = array("price" => 400, "manufacture" => "Devyatkin");
$goods[57] = array("price" => 400, "manufacture" => "Citadel");
$goods[78] = array("price" => 800, "manufacture" => "Agrarnik");
$goods[89] = array("price" => 790, "manufacture" => "Belyi Orel");

function sort_pm($a, $b)
{
  $r1 = strcmp($a["price"], $b["price"]);
  return ($r1 == 0) ? strcmp($a["manufacture"], $b["manufacture"]) : $r1;
}
uasort($goods, "sort_pm");
print_r($goods);

?>


We have added the array with one more element with a price “400” and a manufacturer “Citadel”. Now, after the sorting this element will be higher than “Devyatkin”:

Array
(
    [65] => Array
        (
            [price] => 200
            [manufacture] => Zelina
        )

    [57] => Array
        (
            [price] => 400
            [manufacture] => Citadel
        )

    [45] => Array
        (
            [price] => 400
            [manufacture] => Devyatkin
        )

    [89] => Array
        (
            [price] => 790
            [manufacture] => Belyi Orel
        )

    [78] => Array
        (
            [price] => 800
            [manufacture] => Agrarnik
        )

)