Two-dimensional array ordering in PHP, you can use PHP built-in function uasort ()
"Use the user-defined comparison function to sort the values in the array and keep the index associated"
The callback function is as follows: note that when the return value of the callback function is negative or FALSE, the first parameter of the callback function is in front, and the second argument is arranged in the back
$person = Array (
' num ' => ' 001 ', ' id ' =>6, ' name ' => ' Zhangsan ', ' age ' =>21),
array (' num ' => ') 001 ', ' id ' =>7, ' name ' => ' Ahangsan ', ' age ' =>23 ',
array (' num ' => ' 003 ', ' id ' =>1, ' name ' => ') Bhangsan ', ' age ' =>23),
array (' num ' => ' 001 ', ' id ' =>3, ' name ' => ' Dhangsan ', ' age ' =>23),
);
A negative number or false indicates that the first parameter should be in the former
function sort_by_name ($x, $y) {return
strcasecmp ($x [' name '], $y [' name ']);
}
Use the following:
Uasort ($person, ' sort_by_name ');
A two-dimensional array ordering method for reference and interview purposes is given below:
$array array
//$row Sort By column
//$type sort Type [ASC or DESC]
//return array
function Array_sort ( $array, $row, $type) {
$array _temp = Array ();
foreach ($array as $v) {
$array _temp[$v [$row]] = $v;
}
if ($type = = ' asc ') {
ksort ($array _temp);
} ElseIf ($type = ' desc ') {
krsort ($array _temp);
} else{
} return
$array _temp;
}
=====================================================================
Here, by the way, some of the functions of PHP sort
Sort array sorting generally applies to one-dimensional indexed arrays and does not keep the index
"Rsort array Reverse" and sort usage consistent
Sort values by asort the array and keeping an indexed relationship, generally applies to one-dimensional arrays, keeping index relationships
"Arsort an array and keeping an indexed relationship" is consistent with asort usage
"Ksort array by key name"
"Krsort Arrays in reverse order by key name"
=====================================================================