About the Author
Hi, I’m Chris, a web craftsman making things like Mapalong & Brooklyn Beta with my friends at Analog.
Sorting Multi-Dimensional Arrays in PHP
- Thu, 30 Jun 2011 at 20:57:25 GMT
- 13 comments
- tweets
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:
<?php
$users = array();
$users[] = array('username' => 'shiflett', 'name' => 'Chris Shiflett');
$users[] = array('username' => 'dotjay', 'name' => 'Jon Gibbins');
$users[] = array('username' => 'a', 'name' => 'Andrei Zmievski');
?>
There are a few different ways to create this array. Here's the output of print_r($users)
, so you clearly understand the structure:
Array
(
[0] => Array
(
[username] => shiflett
[name] => Chris Shiflett
)
[1] => Array
(
[username] => dotjay
[name] => Jon Gibbins
)
[2] => Array
(
[username] => a
[name] => Andrei Zmievski
)
)
If I want to sort by username, I first create a separate array of usernames:
<?php
$usernames = array();
foreach ($users as $user) {
$usernames[] = $user['username'];
}
?>
I then use array_multisort()
:
<?php
array_multisort($usernames, SORT_ASC, $users);
?>
Here's the output of print_r($users)
after sorting by username:
Array
(
[0] => Array
(
[username] => a
[name] => Andrei Zmievski
)
[1] => Array
(
[username] => dotjay
[name] => Jon Gibbins
)
[2] => Array
(
[username] => shiflett
[name] => Chris Shiflett
)
)
To sort the array by name instead, I'd do something very similar:
<?php
$names = array();
foreach ($users as $user) {
$names[] = $user['name'];
}
array_multisort($names, SORT_ASC, $users);
?>
Here's the output of print_r($users)
after sorting by name:
Array
(
[0] => Array
(
[username] => a
[name] => Andrei Zmievski
)
[1] => Array
(
[username] => shiflett
[name] => Chris Shiflett
)
[2] => Array
(
[username] => dotjay
[name] => Jon Gibbins
)
)
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:
<?php
// Sort the array by username.
usort($users, function ($a, $b) {
return strcmp($a['username'], $b['username']);
});
?>
If your array is large, or you're concerned about performance, make sure you read Jordi Boggiano's comment.
About this post
Sorting Multi-Dimensional Arrays in PHP was posted on Thu, 30 Jun 2011 at 20:57:25 GMT. Follow me on Twitter.
- Hacker News
- Delicious
Hello! What’s your name?
Want to comment? Please connect with Twitter to join the discussion.
- Previous Post
- Blog History
- Next Post
Feeds
- Blog (6412 readers)
Ads via Carbon
Planet
- Unstructured data is worth the effort when you've got the right tools
- from O'Reilly Radar
- Fluid grids, orientation resolution independence
- from Jeffrey Zeldman
- Four short links: 7 February 2012
- from O'Reilly Radar
- Building The Ecosystem
- from Fred Wilson
- Speaking at DayCamp for Developers
- from Lorna Mitchell
- End users adapting tablets for specific business needs
- from Jonathan Stark
- Relative Date Ranges: Current Week of Prior Year
- from Timothy Boronczyk
Explore the Planet
Fresh Links
- Accept-Charset Is No More
- Twitter ♥ Open Source
- A Beginners Guide to HTML & CSS
- Kern And Burn
- Git Immersion
- Eloquent JavaScript
- Switch to OSM
- The Science of Magic Series
- CartoDB
- HandBrake Batch
Explore Links
13 comments
1. Franco Zeoli said:
2. John Campbell said:
3. Avi Block said:
4. Rolands Atvars said:
5. Nils Luxton said:
6. Jordi Boggiano said:
7. Jim Gaudet said:
8. Nick Yahnke said:
9. Jeroen Fiege said:
10. Joel Caton said:
11. Wil Moore said:
12. Michael Noga said:
13. David Authier said: