Jump to content

Sorting multi-dimensional arrays


tHud
Go to solution Solved by Barand,

Recommended Posts

Hello,

I've been struggling to get array_multisort to work...

 

If I have this array output...

I'd like to sort on the product sub-array.

 

The array is called orders and that number 15 is the specific order id.

Array
(
    [15] => Array
        (
            [clientId] => 1
            [clientName] => Bob
            [o_date] => 2014-01-02
            [o_number] => 1 july-append
            [o_notes] => order on 1 july-append
            [i_date] => 2014-01-17
            [i_number] => 18072014-append
            [i_notes] => invoiced on 18 july 14 -append
            [products] => Array
                (
                    [0] => Array
                        (
                            [id] => 37
                            [prod_id] => 1
                            [product] => door
                            [qty] => 2
                            [cost] => 12.121
                        )

                    [1] => Array
                        (
                            [id] => 38
                            [prod_id] => 2
                            [product] => window
                            [qty] => 3
                            [cost] => 22.5
                        )

                    [2] => Array
                        (
                            [id] => 36
                            [prod_id] => 3
                            [product] => chair
                            [qty] => 1
                            [cost] => 300
                        )

                )

        )

)

So, I've tried a number of variations of this...

array_multisort($orders[$theOrder]['products'], SORT_DESC, SORT_NUMERIC, $orders[$theOrder]['products'], SORT_ASC, SORT_STRING);
array_multisort($orders[$theOrder]['products']['id'], SORT_DESC, SORT_NUMERIC);

etc. etc.
 

- but I seem to get no-where.

Can some-one kindly point out my error, please?

 

Thank you. :) :)

Link to comment
Share on other sites

  • Solution

 

 

I'd like to sort on the product sub-array.

 

On which field?

 

You could use uasort() with a custom sort function, but the best way to do is when you you get the data from the database in the first place (if that is where the array came from originally)

Link to comment
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.