Hi i am facing problem in sorting multidimensional array by different key,such as by date, by category, by weight in any specific order.
I can not order these array by mysql order by feature as i have to implement a tough business logic on mysql output array (data)..
After implementing business logic i found following type of array that need to be sorted by
date asc,category desc,weight asc.
array have size of 10000 or more.
i have already used usort function but it can't resolve issue of fixed ordering in case of same value of sorting elements.
plz help.
Array
(
[0] => Array
(
[categorie] => xyz
[date] => 2012-12-08 19:30
[weight] => 3
[row_id] => 125812
[searchtype] => show
[uitgespeeld] => 0
)
[1] => Array
(
[categorie] => show
[date] => 2012-12-10 20:15
[weight] => 3
[row_id] => 125816
[searchtype] => show
[uitgespeeld] => 0
)
[2] => Array
(
[categorie] => abc
[date] => 2012-12-13 20:30
[weight] => 3
[row_id] => 119151
[searchtype] => show
[uitgespeeld] => 0
)
.......
)
Code i have used for sorting.
usort($temp_group_data, array('className','cmp_weight'));
usort($temp_group_data, array('className','cmp_date'));
function cmp_weight($a, $b) {
if (($a['weight']==$b['weight']) ) {
return 0;
} else if ($a['weight'] >$b['weight']) {
return -1;
} else {
return 1;
}
}
function cmp_date($a, $b) {
if (($a['date']==$b['date']) ) {
return 0;
} else if (strtotime($a['date']) >strtotime($b['date'])) {
return -1;
} else {
return 1;
}
}