Page 1 of 1

array sorting

Posted: Mon Nov 21, 2005 11:04 am
by hessodreamy
I have a multi dimensional array which has gone out of whack and I need to sort it, but am having trouble.

I have an array $dateOrders as such:

Code: Select all

Array
(
    [21/11/2005] => Array
        (
            [orders] => Array
                (
                    [1013] => Array
                        (
                            [uts] => 1132584078
                            [products] => Array
                                (
                                    [RW24] => Array
                                        (
                                            [prodQty] => 1
                                        )
                                )
                            [salePrice] => 30.75
                            [costPrice] => 18.19
                        )

                    [1014] => Array
                        (
                            [uts] => 1132584250
                            [products] => Array
                                (
                                    [DB24] => Array
                                        (
                                            [prodQty] => 1
                                        )
                                )
                            [salePrice] => 41.73
                            [costPrice] => 23.9
                        )

                    [1003] => Array
                        (
                            [uts] => 1132582405
                            [products] => Array
                                (
                                    [DB2] => Array
                                        (
                                            [prodQty] => 1
                                        )
                                )
                            [salePrice] => 27.78
                            [costPrice] => 17.91
                        )
                )

            [saleTotal] => 100.26
            [costTotal] => 60
            [orderTotal] => 3
        )

)
Within each date key in $dateOrders, I would like to sort by $dateOrders[$date]['orders'][$orderID]['uts'] if poss, or $dateOrders[$date]['orders'][$orderID] if its easier.
I've tried using array_multisort which has always worked for these kind of things in the past, by doing this

Code: Select all

foreach($dateOrders as $date=>$dateinfo)
        {
          foreach($dateinfo['orders'] as $orderID=>$orderInfo)
            {
              $timeOrder[$orderID] = $orderInfo['uts'];
            }

      array_multisort($timeOrder, SORT_ASC, $dateOrders[$date]['orders']);
        }
But this removes the associative keys and outputs an array like:

Code: Select all

Array
(
    [21/11/2005] => Array
        (
            [orders] => Array
                (
                    [0] => Array
                        (
                            [uts] => 1132582405
                            [products] => Array
                                (
                                    [DB2] => Array
                                        (
                                            [prodQty] => 1
                                        )
                                )
                            [salePrice] => 27.78
                            [costPrice] => 17.91
                        )

                    [1] => Array
                        (
                            [uts] => 1132584078
                            [products] => Array
                                (
                                    [RW24] => Array
                                        (
                                            [prodQty] => 1
                                        )
                                )
                            [salePrice] => 30.75
                            [costPrice] => 18.19
                        )
...and so on
So its removed the keys from the orders array. I think array_multisort is supposed to do this, but i've tried sort, asort, ksort and they seem to have no effect.

What am I doing wrong?

Posted: Mon Nov 21, 2005 11:30 am
by patrikG
hessodreamy wrote:What am I doing wrong?
You're making it too difficult for yourself. 6-dimensional arrays are way too bloated. Is there any way you can cut down to a two- or maximum three-dimensional array?

Posted: Mon Nov 21, 2005 11:32 am
by hessodreamy
Fair point but I normally do alright with these. Anyway its only the third or fourt level I want to sort by :D

Posted: Mon Nov 21, 2005 12:16 pm
by pickle
array_multisort() is awesomly powerful, but I have to re-learn it every time I decide to use it. How about usort()? You could loop through every 1013, 1014, 1003 element and compare it's UTS sub element.

Posted: Tue Nov 22, 2005 6:13 am
by hessodreamy
Sorted. I've sorted by $dateOrders[$date]['orders'][$orderID] using ksort
like so:

Code: Select all

foreach($dateOrders as $date => $dateInfo) ksort($dateOrders[$date]['orders']);
Turned out to be pretty easy. Thanks for help. :D