Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Sorting Multi-Dimensional Arrays in PHP

DZone's Guide to

Sorting Multi-Dimensional Arrays in PHP

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

Every time I need to sort a multi-dimensional array in PHP, I have to remind myself how to do it. It's not quite as quick and easy to look up as most things, so I'm going to blog a quick example. I've always felt like there must be a better way to do this, so please let me know if there is, and I'll update this post accordingly.

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.

From http://shiflett.org/blog/2011/jun/sorting-multi-dimensional-arrays-in-php

Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}