$val){ //对排序字段值进行过滤 $val[$keys] =str_replace('-','',$val[$keys]); $val[$keys] =str_replace(' ','',$val[$keys]); $val[$keys] =str_replace(':','',$val[$keys]); //将记录中指定的键名放入数组中,如:[0]=>5,[1]=>3,[2]=>6 $keysvalue[] =$val[$keys];//排序字段,如:id 索引=》排序键名 } asort($keysvalue);//按值升序排序,且保持键名与键值之间的索引关系,如:[1]=>3,[0]=>5,[2]=>6 reset($keysvalue);//指针重新指向数组第一个 foreach($keysvalueas$key=>$vals) { $keysort[] =$key;//0=>[1],1=>[0],2=>[2] } $keysvalue=array(); $count=count($keysort);//排序记录数 if(strtolower($type) !='asc'){//降序 for($i=$count-1;$i>=0;$i--) { $keysvalue[] =$array[$keysort[$i]]; } }else{//升序 for($i=0;$i<$count;$i++){ $keysvalue[] =$array[$keysort[$i]]; } } return$keysvalue; } $array=array( array('name'=>'Tom','age'=>'23','like'=>'beer'), array('name'=>'Trump','age'=>'50','like'=>'Food'), array('name'=>'Jack','age'=>'26','like'=>'Travel') ); print_r(array_sort($array,'age')); ?>
运行结果:
Array ( [0] => Array ( [name] => Tom [age] => 23 [like] => beer ) [1] => Array ( [name] => Jack [age] => 26 [like] => Travel ) [2] => Array ( [name] => Trump [age] => 50 [like] => Food ) )