PHP one-dimensional array can be sorted using functions such as sort(), asort(), arsort();
The sorting of PHP two-dimensional array needs to be customized.
The following function sorts a given two-dimensional array according to the specified key value. Let’s look at the function definition first:
Copy code The code is as follows:
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) ;
$index = 0;//Save the subscript unchanged with $k, use $index starting from 0;
foreach ($keysvalue as $k=>$v){
$new_array[$index] = $arr[$k];
$index++;
}
return $new_array;
}
It can sort the two-dimensional array according to the specified key value, and can also specify the ascending or descending sorting method (the default is ascending order). Usage example:
Copy code The code is as follows:
$array = array(
array('name'=>'Js ','date'=>'2014-05-01'),
array('name'=>'Sh','date'=>'2014-04-30'),
array('name'=>'Bj','date'=>'2014-05-02')
);
$arrayList = array_sort($array,'date');
print_r($arrayList);
http://www.bkjia.com/PHPjc/770581.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/770581.htmlTechArticlePHP one-dimensional array can be sorted using functions such as sort(), asort(), arsort(); PHP 2 The ordering of dimensional arrays needs to be customized. The following function is to perform a given two-dimensional array according to the specified key value...