array sorting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hessodreamy
Forum Commoner
Posts: 58
Joined: Wed Apr 20, 2005 8:11 am

array sorting

Post 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?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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?
hessodreamy
Forum Commoner
Posts: 58
Joined: Wed Apr 20, 2005 8:11 am

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
hessodreamy
Forum Commoner
Posts: 58
Joined: Wed Apr 20, 2005 8:11 am

Post 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
Post Reply