RE: [PHP] Sort Array
| From: | tg-php at gryffyndevelopment dot com | Date: | Tue, 18 Jul 2006 14:36:00 +0000 |
| Subject: | RE: [PHP] Sort Array | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-239546@lists.php.net to get a copy of this message | ||
Actually it's going to be a little more complicated than a 'ksort' here I think.
ksort on the main array is going to give you:
array (
"TBA0123451" =>
array("Country"=>"Germany","Model"=>"WS6234"),
"TBA0123452" =>
array("Country"=>"England","Model"=>"WS1234"),
"TBA0123456" =>
array("Country"=>"Singapore","Model"=>"WS8234"),
"TBA0123458" =>
array("Country"=>"Indonesia","Model"=>"WS2234"),
"TBA0123459" =>
array("Country"=>"Vietnam","Model"=>"WS7234")
)
and ksort on the next leve down is going to sort the keys "Country" and "Model"
so all the "Country" elements come before all the "Model" elements, which they
already do.
I'm guessing what's being asked here is to be able to sort the "TBA" level (?)
by the value of the "Model"s then by the value of the "Country"s. So you'd
end up with:
array (
"TBA0123452" =>
array("Country"=>"England","Model"=>"WS1234"),
"TBA0123456" =>
array("Country"=>"Singapore","Model"=>"WS1234"),
"TBA0123458" =>
array("Country"=>"Indonesia","Model"=>"WS2234"),
"TBA0123451" =>
array("Country"=>"Germany","Model"=>"WS6234"),
"TBA0123459" =>
array("Country"=>"Vietnam","Model"=>"WS7234")
)
(Changed Singapore's Model to match England's to illustrate Model then Country sorting)
If this is what the goal is, then it looks like the uasort() function might help. Although I
havn't messed with any of the usort() functions and on the surface they kind of bewilder me,
but I believe that's what might help here. It does a user defined sort while maintaining index
associations.
http://us3.php.net/manual/en/function.uasort.php
Hope that helps.
-TG
= = = Original message = = =
[snip]
I have the array below : How to sort the array by "Model" and "Country?
Thanks
array(
"TBA0123456" =>
array("Country"=>"Singapore","Model"=>"WS8234"),
"TBA0123458" =>
array("Country"=>"Indonesia","Model"=>"WS2234"),
"TBA0123459" =>
array("Country"=>"Vietnam","Model"=>"WS7234"),
"TBA0123452" =>
array("Country"=>"England","Model"=>"WS1234"),
"TBA0123451" =>
array("Country"=>"Germany","Model"=>"WS6234"),
)
[/snip]
http://www.php.net/manual/en/function.ksort.php
___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.