Sorting Multi-Dimensional Arrays in PHP

30 Jun 2011

Every time I need to sort a multi-dimensional array in PHP, I have to look it up. It's not quite as quick and easy to look up as most things, so I'm going to blog a quick example.

Here's a simple array of users:

  1. <?php
  2.  
  3. $users = array();
  4.  
  5. $users[] = array('username' => 'shiflett', 'name' => 'Chris Shiflett');
  6. $users[] = array('username' => 'dotjay', 'name' => 'Jon Gibbins');
  7. $users[] = array('username' => 'a', 'name' => 'Andrei Zmievski');
  8.  
  9. ?>

There are a few different ways to create this array. Here's the output of print_r($users), so you clearly understand the structure:

  1. Array
  2. (
  3.     [0] => Array
  4.         (
  5.             [username] => shiflett
  6.             [name] => Chris Shiflett
  7.         )
  8.  
  9.     [1] => Array
  10.         (
  11.             [username] => dotjay
  12.             [name] => Jon Gibbins
  13.         )
  14.  
  15.     [2] => Array
  16.         (
  17.             [username] => a
  18.             [name] => Andrei Zmievski
  19.         )
  20.  
  21. )

If I want to sort by username, I first create a separate array of usernames:

  1. <?php
  2.  
  3. $usernames = array();
  4.  
  5. foreach ($users as $user) {
  6.     $usernames[] = $user['username'];
  7. }
  8.  
  9. ?>

I then use array_multisort():

  1. <?php
  2.  
  3. array_multisort($usernames, SORT_ASC, $users);
  4.  
  5. ?>

Here's the output of print_r($users) after sorting by username:

  1. Array
  2. (
  3.     [0] => Array
  4.         (
  5.             [username] => a
  6.             [name] => Andrei Zmievski
  7.         )
  8.  
  9.     [1] => Array
  10.         (
  11.             [username] => dotjay
  12.             [name] => Jon Gibbins
  13.         )
  14.  
  15.     [2] => Array
  16.         (
  17.             [username] => shiflett
  18.             [name] => Chris Shiflett
  19.         )
  20.  
  21. )

To sort the array by name instead, I'd do something very similar:

  1. <?php
  2.  
  3. $names = array();
  4.  
  5. foreach ($users as $user) {
  6.     $names[] = $user['name'];
  7. }
  8.  
  9. array_multisort($names, SORT_ASC, $users);
  10.  
  11. ?>

Here's the output of print_r($users) after sorting by name:

  1. Array
  2. (
  3.     [0] => Array
  4.         (
  5.             [username] => a
  6.             [name] => Andrei Zmievski
  7.         )
  8.  
  9.     [1] => Array
  10.         (
  11.             [username] => shiflett
  12.             [name] => Chris Shiflett
  13.         )
  14.  
  15.     [2] => Array
  16.         (
  17.             [username] => dotjay
  18.             [name] => Jon Gibbins
  19.         )
  20.  
  21. )

There are many more uses of array_multisort(), and there are many other useful sorting functions. Please feel free to share some of your favorites in the comments.

Update: If your array isn't too big, and especially if you find it easier to understand, you might prefer usort(). Thanks to Franco Zeoli for this example:

  1. <?php
  2.  
  3. // Sort the array by username.
  4. usort($users, function ($a, $b) {
  5.                   return strcmp($a['username'], $b['username']);
  6.               });
  7.  
  8. ?>

If your array is large, or you're concerned about performance, make sure you read Jordi Boggiano's comment.