[PHP] Sorting Multidimensional Array

[PHP] Sorting Multidimensional Array

Post by ceo » Thu, 02 Nov 2006 02:48:19



Don't do that. :-)

For starters, sorting in PHP is MUCH less efficient than in a DB.

Secondly, getting multi-dimensional (sic) arrays in PHP to sort like
you want generates a ton of traffic here, so it must be hard. :-)

You can do somethig like this:

(SELECT x, y, z FROM real_data
UNION
SELECT 'manually', 'inserted', 'data' as z
)
ORDER BY z

And achieve MUCH better results with far less headache.

If 'z' is indexed in real_data, add another ORDER BY z right before
the UNION -- That will probably make it very fast/easy for the DB to
splice in your manually-inserted 'z' value.

--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://www.yqcomputer.com/ ***
Yeah, I get a buck. So?
 
 
 

[PHP] Sorting Multidimensional Array

Post by larentiu » Thu, 02 Nov 2006 03:05:14

Thanks Richard,

But wouldn't "order by z" interfere with my original order by values?

My code is as follows:

$fields1 = " ID, Title, Label, Location, Start, End, Time, ";
$fields1 .= " Description, Organization, Department, Contact, ";
$fields1 .= " Phone, Email, Global, Board, Committee, Status, TBD_Time ";

$command = " SELECT $fields1, TO_DAYS(End) - TO_DAYS(Start) + 1 AS Days
";
$command .= " FROM $tablename ";
$command .= " $WHERETXT ORDER BY $sortby $sortorder ";


I just need to add a new record, but I have to use PHP because the Start and
End
dates for that record must match the current month...

If I could add the record to the Select statement as you suggest that would
be wonderful.


Keith


----- Original Message -----
From: "Richard *** " < XXXX@XXXXX.COM >
To: "Keith Spiller" < XXXX@XXXXX.COM >
Cc: < XXXX@XXXXX.COM >
Sent: Tuesday, October 31, 2006 10:48 AM
Subject: Re: [PHP] Sorting Multidimensional Array