[UPHPU] sorting multi-dimensional array

Scott Hill llihttocs at gmail.com
Thu Sep 20 10:43:05 MDT 2007


On 9/20/07, Jon Gale <donjuanica at gmail.com> wrote:
>
> On 9/20/07, Wade Preston Shearer <lists at wadeshearer.com> wrote:
> >
> > I have an array, like this:
> >
> > $events[] = array('name' => 'value', 'date' => '2007-09-01');
> > $events[] = array('name' => 'value', 'date' => '2007-09-02');
> >
> >
> > I want to sort $events on $events[]['date];
>
>
> Quick 'n' dirty:
>
> function datesort($a,$b)
> {
>     return strcmp($a['date'],$b['date']);
> }
>
> $events[] = array('name' => 'one', 'date' => '2007-09-01');
> $events[] = array('name' => 'two', 'date' => '2008-09-02');
> $events[] = array('name' => 'three', 'date' => '2007-01-02');
>
> uasort(&$events,'datesort');
>
> print_r($events);
>
> HTH
>
> Jon


To sort duplicate date values, make the following changes to datesort:

function datesort($a,$b)
{
    $retVal = strcmp($a['date'],$b['date']);
    if ($retVal == 0)
        $retVal = strcmp($a['name'],$b['name']);
    return $retVal;
}

-- 
Scott Hill

"May you solve interesting problems" - Author Unknown
"A fanatic is one who can't change his mind and won't change the subject." -
Sir Winston Churchill


More information about the UPHPU mailing list