$arr = array(
array('a'=>1,'b'=>2),
array('a'=>7,'b'=>6),
array('a'=>3,'b'=>3),
array('a'=>6,'b'=>5),
array('a'=>4,'b'=>4),
);
1. 第一种
function sortKey($arr,$key,$order='asc'){
$newArr = array();
foreach($arr as $k=>$v){
$newArr[$k] = $v[$key];
}
$order == 'asc'?asort($newArr):arsort($newArr);
foreach($newArr as $k=>$v){
$newArr[$k] = $arr[$k];
}
return $newArr;
}
$newArr = sortKey($arr,'a','asc');
print_r($newArr);
2. 第二种
function sortKey2($arr,$arr2){
if ($arr['a']==$arr2['a']) return 0;
return ($arr['a']<$arr2['a'])?-1:1;
}
usort($arr,'sortKey2');
print_r($arr);
array('a'=>1,'b'=>2),
array('a'=>7,'b'=>6),
array('a'=>3,'b'=>3),
array('a'=>6,'b'=>5),
array('a'=>4,'b'=>4),
);
1. 第一种
function sortKey($arr,$key,$order='asc'){
$newArr = array();
foreach($arr as $k=>$v){
$newArr[$k] = $v[$key];
}
$order == 'asc'?asort($newArr):arsort($newArr);
foreach($newArr as $k=>$v){
$newArr[$k] = $arr[$k];
}
return $newArr;
}
$newArr = sortKey($arr,'a','asc');
print_r($newArr);
2. 第二种
function sortKey2($arr,$arr2){
if ($arr['a']==$arr2['a']) return 0;
return ($arr['a']<$arr2['a'])?-1:1;
}
usort($arr,'sortKey2');
print_r($arr);