RE: [PHP] Sorting multidimensional arrays..
| From: | Ford, Mike [LSS] | Date: | Mon, 03 Feb 2003 13:44:05 +0000 |
| Subject: | RE: [PHP] Sorting multidimensional arrays.. | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-134107@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Chad Day [mailto:cday@atpco.com]
> Sent: 30 January 2003 19:29
>
> I'm struggling with array_multisort, was hoping someone could
> give me some
> help.
>
> I have a multidim array, such as:
>
> $myarray[0][FIRSTNAME] = JOE
> $myarray[1][FIRSTNAME] = TIM
> $myarray[2][FIRSTNAME] = BOB
>
> $myarray[0][LASTNAME] = SMITH
> $myarray[1][LASTNAME] = BROWN
> $myarray[2][LASTNAME] = JONES
>
> $myarray[0][EXTENSION] = 2000
> $myarray[1][EXTENSION] = 4000
> $myarray[2][EXTENSION] = 1000
>
> I was trying array_multisort($myarray[EXTENSION],
> SORT_NUMERIC, SORT_DESC),
You won't be able to use array_multisort() to do this, as your indexes are
the wrong way round. It looks to me like the only solution is using usort()
on $myarray, with a callback function that sorts on the [EXTENSION] element;
so, something like:
function compare($a, $b) {
return $a[EXTENSION]==$b[EXTENSION]?0:($a[EXTENSION]<$b[EXTENSION]?-1:1)
}
usort($myarray, "compare");
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@lmu.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211