Home » Php » Sort php multidimensional array by sub-value

Sort php multidimensional array by sub-value

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have this array

Array
(
[data] => Array
    (
        [0] => Array
            (

                [id] => 1293005125
                [viewed] => TRUE
                [active] => TRUE
                [time] => December 22, 2010 13:00 hours
                [timestamp] => 1293006034
                [initial_timestamp] => 1293005125
                [user] => administrator
            )

        [1] => Array
            (

                [mid] => 1293001908
                [viewed] => TRUE
                [active] => TRUE
                [time] => December 22, 2010 13:00 hours
                [timestamp] => 1293001908
                [initial_timestamp] => 1293001908
                [user] => administrator
            )

        [2] => Array
            (

                [mid] => 1293009999
                [viewed] => TRUE
                [active] => TRUE
                [time] => December 22, 2010 13:00 hours
                [timestamp] => 1293009999
                [initial_timestamp] => 1293009999
                [user] => administrator
            )

        [3] => Array
            (

                [mid] => 1293006666
                [viewed] => TRUE
                [active] => TRUE
                [time] => December 22, 2010 13:00 hours
                [timestamp] => 1293006666
                [initial_timestamp] => 1293006666
                [user] => administrator
            )

        [4] => Array
            (

                [mid] => 1293005125
                [viewed] => TRUE
                [active] => TRUE
                [time] => December 22, 2010 13:00 hours
                [timestamp] => 1293006125
                [initial_timestamp] => 1293005125
                [user] => administrator2
            )


    )

Now I would like to sort this array by [mid]
How do I do this?

Currently I sort this in a foreach loop
There has to be a better way

EDIT
I hoped to output something like

[mid] key => array value

Thanks

How to&Answers:

You can use the usort function.

function cmp($a, $b) {
        return $a["mid"] - $b["mid"];
}
usort($arr, "cmp");

See it

Answer:

The other solution is using array_multisort

<?php
// Obtain a list of columns
foreach ($data as $key => $row) {
    $mid[$key]  = $row['mid'];
}

// Sort the data with mid descending
// Add $data as the last parameter, to sort by the common key
array_multisort($mid, SORT_DESC, $data);
?>

Answer:

Update

I recently answered this question in a much more capable manner in the “definitive” topic on sorting multidimensional arrays. The answer below targets old versions of PHP (5.2 and earlier); while the concept is sound, nowadays there are much better ways of doing things. Read the answers on other question instead.

Original (very outdated) answer

usort is there for exactly this situation. If you also need keys to be preserved, the appropriate function would be uasort.

For example:

usort($array, create_function('$a, $b',
   'if ($a["mid"] == $b["mid"]) return 0; return ($a["mid"] < $b["mid"]) ? -1 : 1;'));

Of course if you don’t mind, you can declare the comparison function properly:

function compareMid($a, $b)
{
    if ($a['mid'] == $b['mid']) {
        return 0;
    }
    return ($a['mid'] < $b['mid']) ? -1 : 1;
}

And use it like this:

usort($array, 'compareMid');

All of this is in the documentation.