RE: Sort Array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux