0

I have this example array, and I would like to sort each first level item (66000, 66001, 66002) in this order: New Store first in the list, Store Relocation second in the list, and I didn't include Underperforming Stores in the example array, but those would be last. those then need to be sorted by open date How can I sort them this way?

I believe I need to use usort to accomplish this, but I don't really understand the usort function, can I get help with this?

Here is my function so far...

usort($mainpgArr, function($a, $b){

});

Example Original Array:

Array(
    [66000] => Array(
        [January] => Array(
            [status] => New Store
            [sales] => 100.00
            [open] => 2013-05-01
        )
        [February] => Array(
            [status] => New Store
            [sales] => 200.00
            [open] => 2013-05-01
        )
        [March] => Array(
            [status] => New Store
            [sales] => 140.00
            [open] => 2013-05-01
        )
    )
    [66001] => Array(
        [January] => Array(
            [status] => Store Relocation
            [sales] => 3400.00
            [open] => 2013-07-01
        )
        [February] => Array(
            [status] => Store Relocation
            [sales] => 1340.00
            [open] => 2013-07-01
        )
        [March] => Array(
            [status] => Store Relocation
            [sales] => 1550.00
            [open] => 2013-07-01
        )
    )
    [66002] => Array(
        [January] => Array(
            [status] => New Store
            [sales] => 1050.00
            [open] => 2013-01-01
        )
        [February] => Array(
            [status] => New Store
            [sales] => 1009.00
            [open] => 2013-01-01
        )
        [March] => Array(
            [status] => New Store
            [sales] => 1020.00
            [open] => 2013-01-01
        )
    )
)

Example Final Array

Array(
    [66002] => Array(
        [January] => Array(
            [status] => New Store
            [sales] => 1050.00
            [open] => 2013-01-01
        )
        [February] => Array(
            [status] => New Store
            [sales] => 1009.00
            [open] => 2013-01-01
        )
        [March] => Array(
            [status] => New Store
            [sales] => 1020.00
            [open] => 2013-01-01
        )
    )
    [66000] => Array(
        [January] => Array(
            [status] => New Store
            [sales] => 100.00
            [open] => 2013-05-01
        )
        [February] => Array(
            [status] => New Store
            [sales] => 200.00
            [open] => 2013-05-01
        )
        [March] => Array(
            [status] => New Store
            [sales] => 140.00
            [open] => 2013-05-01
        )
    )
    [66001] => Array(
        [January] => Array(
            [status] => Store Relocation
            [sales] => 3400.00
            [open] => 2013-07-01
        )
        [February] => Array(
            [status] => Store Relocation
            [sales] => 1340.00
            [open] => 2013-07-01
        )
        [March] => Array(
            [status] => Store Relocation
            [sales] => 1550.00
            [open] => 2013-07-01
        )
    )
)
Get Off My Lawn
  • 27,770
  • 29
  • 134
  • 260
  • What exactly about `usort` do you not understand? There are examples on php.net, have you read them? Have you tried something simple to get your feet wet first? – Jon Aug 19 '13 at 19:05

2 Answers2

0

All you need to do in usort is define how any 2 given items should be ordered.

In your case it would be easier if your array data was tidied up a bit, by taking the status out of the month data. As is, we'll have to assume it will be the same for all months.

With that assumption made we will use the first months' status for our comparison.

usort($mainpgArr, function($a, $b){
   if ($a[0]['status'] == $b[0]['status']) {
       // status equal so sort by open date
       if ($a[0]['open'] == $b[0]['open']) {
           return 0;
       } else {
           return ($a[0]['open'] > $b[0]['open']) ? 1 : -1;
       } 
   } else {
       // sorting by status
       // we can use a simple comparison operation as your desired order happens to be alphabetical
       return ($a[0]['status'] > $b[0]['status']) ? 1 : -1;
   }
});

If the keys are important to you you'll need to look at using uasort as usort doesn't preserve keys.

rc_luke
  • 121
  • 5
-1

You aren't doing it correct.

This is really simple: http://www.php.net/manual/en/function.ksort.php

ksort($mainpgArr);

And that's all folks. Now $mainpgArr is sorted how you want it. To sort the multidimensional part would be with asort();


sorry, misunderstood the question. Okay, you need to sort the keys with ksort(). Then loop through the whole thing creating a new array with the sorted keys while asort() the indexes..

example:

ksort($mainpgArr);
foreach($mainpgArr as $key => $value) {
  asort($mainpgArr[$key];
}

That should do it. sorry I wasn't more specific earlier. (I haven't tested this, but should work)

Kevin Florida
  • 6,313
  • 3
  • 20
  • 20
  • What? Is this supposed to sort correctly using magical pixie dust? – Jon Aug 19 '13 at 19:04
  • No magic pixie dust dude, asort() sorts an array alphabetically and arsort() sorts it in reverse. Real simple. I will expand my answer since it is multi-dimensional – Kevin Florida Aug 19 '13 at 19:06
  • 1
    While expanding the answer, give some emphasis to the fact that `asort` and `arsort` will never be able to work here because a) this array is multidimensional and b) they cannot use multiple criteria at the same time. Dude. – Jon Aug 19 '13 at 19:10