Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Sorting multidimensional arrays?

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #28033
    jlizarraga
    Member

    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.

    #71045
    john010117
    Member

    http://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($valSORT_ASC);
    ?>

    (note: untested)

    #71116
    jlizarraga
    Member

    Thanks for the response, but that didn’t work either. I tried it with and without the reference (&) operator.

    #71148
    john010117
    Member

    I 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.

Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘Back End’ is closed to new topics and replies.