arr = [
0 => [key=> val, key2=> val2]
1 => [key=> val2, key2=> val22]
]
[
[ k=>2, k2=>3 ], [ k=>22, k2=>5 ], [ k=>23, k2=>4 ]
]
[
[ k=>2, k2=>3 ], [ k=>23, k2=>4 ], [ k=>22, k2=>5 ]
]
$key = 'key2';
uasort($arr, function($a,$b) use($key){
if ($a[$key] == $b[$key]) {
return 0;
}
return ($a[$key] < $b[$key]) ? -1 : 1;
});
//update
/*
@param array $arr array to sort
@param string $key key
@param int $direction 1 or -1
*/
function sortByKey($arr, $key, $direction = 1){
uasort($arr, function($a,$b) use($key, $direction){
if ($a[$key] == $b[$key]) {
return 0;
}
return ($a[$key] < $b[$key]) ? -1*$direction : 1*direction;
});
return $arr
}