FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » multi-dimensional array sorting by index (like a table)
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: multi-dimensional array sorting by index (like a table) [message #176356 is a reply to message #176350] Tue, 27 December 2011 23:16 Go to previous messageGo to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma:
Senior Member
On Tue, 27 Dec 2011 15:25:29 -0500, Michael Joel wrote:

> I am guessing there is no php function to handle sorting an array as you
> could a database? Meaning I want a multi-dimensional array sorted by
> index 0, and "sub sorting" (whatever you call it) using index 4?

Yes, you write a user sort that compares two elements, something like:

function mysort($a, $b) {
if ($a[0] > $b[0]) return 1;
if ($a[0] < $b[0]) return -1;
if ($a[4] > $b[4]) return 1;
if ($a[4] < $a[4]) return -1;
return 0;
}

then

usort($arr, "mysort");

You might want error handling in mysort to make sure that $a and $b are
both actually arrays of at least 5 elements size.

Rgds

Denis McMahon
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: standard easy way to set up a registration/logon cookie?
Next Topic: is the php.net server down?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Nov 27 05:39:14 GMT 2024

Total time taken to generate the page: 0.04945 seconds