function sort_arr_of_obj($array, $sortby, $direction='asc') {
$sortedArr = array();
$tmp_Array = array();
foreach($array as $k => $v) {
$tmp_Array[] = strtolower($v[$sortby]);
}
if($direction=='asc'){
asort($tmp_Array);
}else{
arsort($tmp_Array);
}
foreach($tmp_Array as $k=>$tmp){
$sortedArr[] = $array[$k];
}
return $sortedArr;
}
$myArray=array(0=>array('size'=>5,'name'=>'Five'),1=>array('size'=>3,'name'=>'Three'),2=>array('size'=>9,'name'=>'Nine'));
$myArray=sort_arr_of_obj($myArray,'size','asc');
$sortedArr = array();
$tmp_Array = array();
foreach($array as $k => $v) {
$tmp_Array[] = strtolower($v[$sortby]);
}
if($direction=='asc'){
asort($tmp_Array);
}else{
arsort($tmp_Array);
}
foreach($tmp_Array as $k=>$tmp){
$sortedArr[] = $array[$k];
}
return $sortedArr;
}
$myArray=array(0=>array('size'=>5,'name'=>'Five'),1=>array('size'=>3,'name'=>'Three'),2=>array('size'=>9,'name'=>'Nine'));
$myArray=sort_arr_of_obj($myArray,'size','asc');
No comments:
Post a Comment