Jump to content


Photo

help using array_multisort


  • Please log in to reply
No replies to this topic

#1 UniAce

UniAce
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 31 July 2006 - 08:32 PM

Hello,
I'm working on my bird-watching website and I have a multi-dimensional array that consists of a number of 'column' arrays, each of which has an equal number of 'rows.'  I will paste an example below.

I need to sort this array first by one column, then by another, preferably with the option of whether to do each in ascending or descending order.  For example, I'd like to sort the whole thing by bird_commonality descending and then by bird_name ascending, and to have all the 'rows' remain intact.

I don't have the wherewithal right now to do anything with mysql.  It looked like array_multisort should do the trick, but I'm having trouble with it.  Here is the call that I use:

array_multisort($birdList["bird_commonality"], SORT_DESC, $birdList["bird_name"], SORT_ASC, $birdList["bird_num"]);


Here is a sample of the array $birdList as it starts out:


Array
(
    [0] => 71
    [bird_num] => Array
        (
            [1] => 1
            [2] => 2
            [3] => 3
            [4] => 4
            [5] => 5
            [6] => 6
        )

    [bird_name] => Array
        (
            [1] => Allen's Hummingbird
            [2] => American Crow
            [3] => American Kestrel
            [4] => American Robin
            [5] => Anna's Hummingbird
            [6] => Ash-throated Flycatcher
        )

    [bird_commonality] => Array
        (
            [1] => vc
            [2] => c
            [3] => r
            [4] => r
            [5] => vc
            [6] => r
        )

    [bird_size] => Array
        (
            [1] => vs
            [2] => l
            [3] => m
            [4] => m
            [5] => vs
            [6] => s
        )


    [bird_page] => Array
        (
            [1] => allenhummer.htm
            [2] => crow.htm
            [3] => <>
            [4] => robin.htm
            [5] => annahummer.htm
            [6] => <>
        )

    [bird_thumbnail1] => Array
        (
            [1] => allenhumm-F1.gif
            [2] => crowA1.gif
            [3] =>
            [4] => robinA1.gif
            [5] => annahumm-nestA1.gif
            [6] =>
        )

)


And here is a sample of what it looks like when after the call to array_multisort:

Array
(
    [0] => 71
    [bird_num] => Array
        (
            [0] => 1
            [1] => 5
            [2] => 13
            [3] => 30
            [4] => 31
            [5] => 38
            [6] => 47
        )

    [bird_name] => Array
        (
            [0] => Allen's Hummingbird
            [1] => Anna's Hummingbird
            [2] => Brewer's Blackbird
            [3] => House Finch
            [4] => House Sparrow
            [5] => Mourning Dove
            [6] => Pigeon (Rock Dove)
        )

    [bird_commonality] => Array
        (
            [0] => vc
            [1] => vc
            [2] => vc
            [3] => vc
            [4] => vc
            [5] => vc
            [6] => vc
        )

    [bird_size] => Array
        (
            [1] => vs
            [2] => l
            [3] => m
            [4] => m
            [5] => vs
            [6] => s
        )

    [bird_page] => Array
        (
            [1] => allenhummer.htm
            [2] => crow.htm
            [3] => <>
            [4] => robin.htm
            [5] => annahummer.htm
            [6] => <>
        )

    [bird_thumbnail1] => Array
        (
            [1] => allenhumm-F1.gif
            [2] => crowA1.gif
            [3] =>
            [4] => robinA1.gif
            [5] => annahumm-nestA1.gif
            [6] =>
        )

)

'Columns' like bird_size and bird_page have not been re-ordered along with the two I've sorted by.  Maybe it has something to do with my column arrays starting at 1 instead of 0?  Or maybe I'm doing something else wrong?  Any help would be greatly appreciated.  Thank you




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users