- <?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);
- echo '<pre />';
- ?>
运行结果:
本文转自许琴 51CTO博客,原文链接:http://blog.51cto.com/xuqin/1052396,如需转载请自行联系原作者