Jump to content

How to sort a multidimensional array


Guardian-Mage

Recommended Posts

Array
(
    [0] => Array
        (
            [0] => Advanced PHP for Web Professionals 
            [1] => 
            [2] => Compiled HTML
            [3] => 2002
            [4] => 
        )

    [1] => Array
        (
            [0] => Creating Cool Web Sites With HTML, XHTML, And CSS 
            [1] => 
            [2] => Portable Document Format
            [3] => 2004
            [4] => 
        )

    [2] => Array
        (
            [0] => CSS Hacks & Filters - Making Cascading Stylesheets Work 
            [1] => 
            [2] => Portable Document Format
            [3] => 2005
            [4] => 
        )

    [3] => Array
        (
            [0] => CSS Web Design for Dummies 
            [1] => 
            [2] => Portable Document Format
            [3] => 2005
            [4] => 
        )

    [4] => Array
        (
            [0] => HTML 4 For Dummies, 5th Edition 
            [1] => 
            [2] => Portable Document Format
            [3] => 2005
            [4] => 
        )

    [5] => Array
        (
            [0] => HTML In 10 Simple Steps Or Less 
            [1] => 
            [2] => Portable Document Format
            [3] => 2004
            [4] => 
        )

    [6] => Array
        (
            [0] => HTML Mastery - Semantics, Standards, And Styling 
            [1] => 
            [2] => Portable Document Format
            [3] => 2006
            [4] => 
        )

    [7] => Array
        (
            [0] => HTML, XHTML, & CSS Bible   
            [1] => 3rd Edition
            [2] => Portable Document Format
            [3] => 2004
            [4] => Brian Pfaffenberger
        )

)

 

How would I sort the above array by the date field ([3]). I have read about arsort and sort and all of them but I don't quite understand it. Help is greatly appreciated.

Link to comment
Share on other sites

I prefer to use usort() (or uasort() if you need to maintain index associations). This is especially helpful with multidimensional arrays where you may need to add more logic than just simple sorting.

 

<?php

function sortByDate($a, $b)
{
   if ($a[3] == $b[3]) {
        return 0;
    }
    return ($a[3] < $b[3]) ? -1 : 1;
}

usort($theArray, 'sortByDate');

?>

 

EDIT: Or you can make it a one liner like so

 

<?php

function sortByDate($a, $b)
{
    return ($a[3] == $b[3])?0:(($a[3] < $b[3]) ? -1 : 1);
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.