二维数组按照指定键值排序

$array = array(

array('name'=>'手机','brand'=>'诺基亚','price'=>1050),

array('name'=>'笔记本电脑','brand'=>'lenovo','price'=>4300),

array('name'=>'剃须刀','brand'=>'飞利浦','price'=>3100),

array('name'=>'跑步机','brand'=>'三和松石','price'=>4900),

array('name'=>'手表','brand'=>'卡西欧','price'=>960),

array('name'=>'液晶电视','brand'=>'索尼','price'=>6299),

array('name'=>'激光打印机','brand'=>'惠普','price'=>1200)

);


function arr_sort($array,$key,$order="asc"){//asc是升序 desc是降序

$arr_nums=$arr=array();

foreach($array as $k=>$v){

$arr_nums[$k]=$v[$key];

}


if($order=='asc'){

asort($arr_nums);

}else{

arsort($arr_nums);

}

foreach($arr_nums as $k=>$v){

$arr[$k]=$array[$k];

}

return $arr;

}

echo "
";

var_dump(arr_sort($array,'price'));

你可能感兴趣的:(php)