<?php
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);
reset($keysvalue);
foreach ($keysvalue as $k=>$v){
$new_array[$k] = $arr[$k];
return $new_array;
}
$array = array(
array('name'=>'手機','brand'=>'諾基亞','price'=>300),
array('name'=>'筆記本電腦','brand'=>'lenovo','price'=>500),
array('name'=>'刮胡刀','brand'=>'飛利浦','price'=>200),
array('name'=>'跑步機','brand'=>'三和松石','price'=>400),
array('name'=>'手表','brand'=>'卡西歐','price'=>900),
);
$ShoppingList = array_sort($array,'price');
echo '<pre />';
print_r($ShoppingList);
?>
運作結果:
<a target="_blank" href="http://blog.51cto.com/attachment/201211/102348816.jpg"></a>
本文轉自許琴 51CTO部落格,原文連結:http://blog.51cto.com/xuqin/1052396,如需轉載請自行聯系原作者