PHP One-dimensional arrays can be sorted by sort (), Asort (), Arsort () and other functions;
The ordering of PHP two-dimensional arrays needs to be customized.
The following function is to sort a given two-dimensional array according to the specified key value, and first look at the function definition:
Copy the Code code 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 subscript unchanged with $k, subscript starting from 0 with $index;
foreach ($keysvalue as $k = = $v) {
$new _array[$index] = $arr [$k];
$index + +;
}
return $new _array;
}
It can sort the two-dimensional arrays by the specified key values, or you can specify ascending or descending sorting (the default is ascending), and the usage example:
Copy the Code code 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.html www.bkjia.com true http://www.bkjia.com/PHPjc/770581.html techarticle php One-dimensional arrays can be sorted by sort (), Asort (), Arsort (), and the ordering of PHP two-dimensional arrays needs to be customized. The following function is for a given two-dimensional array to follow the specified key value into ...