Jump to content

Recommended Posts

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:

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


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


[code]
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] =>
        )

)
[/code]


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

[code]
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] =>
        )

)
[/code]

'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
Link to comment
https://forums.phpfreaks.com/topic/16140-help-using-array_multisort/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.