php - Sort Multidimensional array by a defined order -


this question has answer here:

i have example array, , sort each first level item (66000, 66001, 66002) in order: new store first in list, store relocation second in list, , didn't include underperforming stores in example array, last. need sorted open date how can sort them way?

i believe need use usort accomplish this, don't understand usort function, can this?

here function 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         )     ) ) 

all need in usort define how 2 given items should ordered.

in case easier if array data tidied bit, taking status out of month data. is, we'll have assume same months.

with assumption made use first months' status our comparison.

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

if keys important you'll need look @ using uasort usort doesn't preserve keys.


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -