- This topic is empty.
-
AuthorPosts
-
February 16, 2010 at 6:28 pm #28033jlizarragaMember
Hi all,
I’m having trouble figuring out how to sort arrays within arrays. I’ve tried asort($array) and array_multisort($array), but my multidimensional array remains unchanged, so I’m guessing I’m not using them right. Here is what my array looks like via print_r():
Code:Array
(
[new] => Array
(
[year] => Array
(
[2010] => 2010
[2009] => 2009
[2007] => 2007
[2008] => 2008
)[bodystyle] => Array
(
[Coupe] => Coupe
[Van] => Van
[SUV] => SUV
[Sedan] => Sedan
[Hatchback] => Hatchback
[Pickup] => Pickup
[Convertible] => Convertible
)[model] => Array
(
[Civic Coupe] => Civic Coupe
[Odyssey] => Odyssey
[CR-V] => CR-V
[Accord Sedan] => Accord Sedan
[Accord Coupe] => Accord Coupe
[Civic Sedan] => Civic Sedan
[Civic GX] => Civic GX
[Pilot] => Pilot
[Element] => Element
[Insight] => Insight
[Fit] => Fit
[Accord Crosstour] => Accord Crosstour
[Civic Hybrid] => Civic Hybrid
[Ridgeline] => Ridgeline
[Civic Si Coupe] => Civic Si Coupe
[S2000] => S2000
))
[used] => Array
(
[year] => Array
(
[2007] => 2007
[2003] => 2003
[2004] => 2004
[2006] => 2006
[2009] => 2009
[1999] => 1999
[2008] => 2008
[2005] => 2005
[2010] => 2010
[2000] => 2000
)[bodystyle] => Array
(
[Sedan] => Sedan
[SUV] => SUV
[Van] => Van
[Coupe] => Coupe
[Hatchback] => Hatchback
[Pickup] => Pickup
)[model] => Array
(
[Accord Sedan] => Accord Sedan
[Civic Sedan] => Civic Sedan
[Pilot] => Pilot
[Civic Hybrid] => Civic Hybrid
[Odyssey] => Odyssey
[Element] => Element
[Accord Coupe] => Accord Coupe
[Insight] => Insight
[CR-V] => CR-V
[Civic] => Civic
[Accord] => Accord
[Civic Si Sedan] => Civic Si Sedan
[Ridgeline] => Ridgeline
[Fit] => Fit
))
)
I’m trying to sort the nested "year", "bodystyle", and "model" arrays in ascending order. Can anyone point me in the right direction? In the meantime, I am sorting those arrays with a loop like so:
Code:foreach($array as &$type){
foreach($type as &$field){
asort($field);
}
}I’d much rather learn the proper way to sort the whole thing all at once, though.
Thanks a bunch.
February 17, 2010 at 12:44 am #71045john010117Memberhttp://php.net/array_multisort
I believe that’s what you’re looking for.
So, it’ll be something like:
Code:<?php
foreach ($array as $key => $val)
{
array_multisort($val, SORT_ASC);
} ?>(note: untested)
February 18, 2010 at 4:36 pm #71116jlizarragaMemberThanks for the response, but that didn’t work either. I tried it with and without the reference (&) operator.
February 19, 2010 at 12:41 am #71148john010117MemberI need details – how did it exactly not work? Put print_r($array); after the foreach loop and see what it outputs and copy & paste it here.
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.