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():
[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:
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.
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():
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:
I'd much rather learn the proper way to sort the whole thing all at once, though.
Thanks a bunch.
I believe that's what you're looking for.
So, it'll be something like:
[code=php]<?php
foreach ($array as $key => $val)
{
array_multisort($val, SORT_ASC);
} ?>[/code]
(note: untested)