Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort multi-dimensional array by specific key

Tags:

php

sorting

ksort

I have an array:

Array (     [0] => stdClass Object (         [user_id] => 1         [ID] => 1         [user_login] => admin         [display_name] => admin         [user_email] => [email protected]         [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] => [email protected]         [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] => [email protected]         [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] => [email protected]         [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] => [email protected]         [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?

like image 448
Himmators Avatar asked Oct 26 '10 09:10

Himmators


People also ask

How do you sort a multi dimensional array by key?

array_multisort() can be used to sort several arrays at once, or a multi-dimensional array by one or more dimensions. Associative (string) keys will be maintained, but numeric keys will be re-indexed. Note: If two members compare as equal, they retain their original order.

How do you sort an array of associative arrays by the value of a given key in PHP?

The arsort() function sorts an associative array in descending order, according to the value. Tip: Use the asort() function to sort an associative array in ascending order, according to the value. Tip: Use the krsort() function to sort an associative array in descending order, according to the key.

Which function sort multiple array at once?

The array_multisort() function returns a sorted array. You can assign one or more arrays. The function sorts the first array, and the other arrays follow, then, if two or more values are the same, it sorts the next array, and so on.

How do I sort a key in PHP?

Answer: Use the PHP ksort() and krsort() function The PHP ksort() and krsort() functions can be used for sorting an array by key.


2 Answers

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)     {         ... 
like image 148
Repox Avatar answered Oct 02 '22 03:10

Repox


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; } 
like image 36
Mike C Avatar answered Oct 02 '22 03:10

Mike C