php對二維陣列中某一欄位進行排序
$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)
);
$ShoppingList = array_sort($array,'price');
echo "<pre>";
print_r($ShoppingList);
function array_sort($arr,$keys,$type='asc'){
$keysvalue = $new_array = array();
foreach ($arr as $k=>$v){
$keysvalue[$k] = $v[$keys];
}
if($type == 'asc'){
asort($keysvalue);
}else{
arsort($keysvalue);
}
var_dump($keysvalue);
reset($keysvalue);
var_dump($keysvalue);
foreach ($keysvalue as $k=>$v){
$new_array[$k] = $arr[$k];
}
return $new_array;
}