37

I have an array:

Array (
    [0] => stdClass Object (
        [user_id] => 1
        [ID] => 1
        [user_login] => admin
        [display_name] => admin
        [user_email] => webbinformation@nacka.se
        [meta_value] => a:1:{s:13:\"administrator\";s:1:\"1\";}
    )
    [1] => stdClass Object (
        [user_id] => 4
        [ID] => 4
        [user_login] => ungtinflytande
        [display_name] => ungtinflytande
        [user_email] => klarakviberg@gmail.com
        [meta_value] => a:1:{s:13:\"administrator\";s:1:\"1\";}
    )
    [2] => stdClass Object (
        [user_id] => 5
        [ID] => 5
        [user_login] => inflytandepilot
        [display_name] => inflytandepilot
        [user_email] => hildalundgren@hotmail.com
        [meta_value] => a:1:{s:6:\"author\";s:1:\"1\";}
    )
    [3] => stdClass Object (
        [user_id] => 11
        [ID] => 11
        [user_login] => matsbohman
        [display_name] => matsbohman
        [user_email] => mats.bohman@nacka.se
        [meta_value] => a:1:{s:6:\"editor\";s:1:\"1\";}
    )
    [4] => stdClass Object (
        [user_id] => 12
        [ID] => 12
        [user_login] => klarakviberg
        [display_name] => klarakviberg
        [user_email] => nastasteg@nacka.se
        [meta_value] => a:1:{s:13:\"administrator\";s:1:\"1\";}
    )
)

...that I wanna sort by the display_name key. I currently print it like this:

foreach ($blogusers as $bloguser) {
    ...
}

How do I do this?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Himmators
  • 14,278
  • 36
  • 132
  • 223

7 Answers7

47

You would use usort() - http://php.net/usort

My suggestion would be:

    function cmp($a, $b)
    {
        return strcmp($a->display_name, $b->display_name);
    }

    usort($blogusers, "cmp");

    foreach ($blogusers as $bloguser)
    {
        ...
Repox
  • 15,015
  • 8
  • 54
  • 79
  • usort() is the way to go for sure. Is strcmp the right function if one were to want to sort on, say, a float? My guess would be separate comparators functions depending on type might be a good idea... – jsh Nov 14 '13 at 15:56
17

See usort: http://php.net/manual/en/function.usort.php

usort($array, "my_cmp");

function my_cmp($a, $b) {
  if ($a->display_name == $b->display_name) {
    return 0;
  }
  return ($a->display_name < $b->display_name) ? -1 : 1;
}
Mike C
  • 1,808
  • 15
  • 17
10

I have find answer at https://joshtronic.com/2013/09/23/sorting-associative-array-specific-key/

function sortBy($field, &$array, $direction = 'asc')
{
    usort($array, create_function('$a, $b', '
        $a = $a["' . $field . '"];
        $b = $b["' . $field . '"];

        if ($a == $b)
        {
            return 0;
        }

        return ($a ' . ($direction == 'desc' ? '>' : '<') .' $b) ? -1 : 1;
    '));

    return true;
}

And now call this function by specific array key.

$newArray = sortBy('display_name',   $blogusers);

And if sort in asc/desc just add one argument,

sortBy('display_name',   $blogusers, 'desc');
Kishan Chauhan
  • 1,216
  • 1
  • 12
  • 19
3

A simpler way would be ...

$key = array_column($blogusers, 'display_name');
array_multisort($key, SORT_ASC, $blogusers]

foreach ($blogusers as $bloguser) ...
Blackwolf
  • 93
  • 1
  • 12
0

Your array looks like the result of a database query. If this is a case, let the database do the sorting: just append ORDER BY display_name to the query.

ax.
  • 58,560
  • 8
  • 81
  • 72
-1

Take a look at following article. It does describe how to use usort() and also describes how to use create_function() so that you can use single function to sort on different fields (with required direction asc or desc).

http://phpave.com/sorting-associative-array-specific-key/

Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
Shriganesh Shintre
  • 2,428
  • 3
  • 17
  • 16
-1

In case you are stuck with error or your array doesn't sort you can give this a try as this works for me-

usort($blogusers,function ($a, $b)
{
    return strcmp($a["display_name"], $b["display_name"]);
});