32

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

0

3 Answers 3

55

You can use the usort function.

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

See it

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, but i found the examples a bit difficult to understand
@RisingSum: I've updated my answer with a working link.
As of 2013 (PHP 5+), we can nest the function inside the usort call as such: usort($arr, function($a, $b) { return $a["mid"] - $b["mid"; });
Here is the "See it" link from archive.org web.archive.org/web/20130429091044/http://ideone.com:80/cW5xZ
35

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);
?>

Comments

5

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.