The
was discovered yesterday to sort the array, sorted by time, but PHP did not have this function in it, so it was found on the web, the first argument was an array, the second was the element to sort, the third was sort,
and the following was PHP Two-dimensional array sorting code
function ArraySort ($arr, $keys, $type = ' asc ') {
$keysvalue = $new _array = Array ();
foreach ($arr as $k => $v) {
$keysvalue [$k] = $v [$keys];
}
$type = = ' ASC '? Asort ($keysvalue): Arsort ($keysvalue);
Reset ($keysvalue);
foreach ($keysvalue as $k => $v) {
$new _array[$k] = $arr [$k];
}
return $new _array;
}
$arr [] = Array ("name" => "1", "Time" =>1);
Array_multisort (array1,sorting order, sorting type,array2,array3. is a function that sorts multiple arrays or multidimensional arrays.
<?php
function My_sort ($arrays, $sort _key, $sort _order=sort_asc, $sort _type=sort_numeric) {
if (Is_array ($arrays)) {
foreach ($arrays as $array) {
if (Is_array ($array)) {
$key _arrays[] = $array [$sort _key];
}else{
return false;
}
}
}else{
return false;
}
Array_multisort ($key _arrays, $sort _order, $sort _type, $arrays);
return $arrays;
}
$person = Array (
Array (' ID ' =>1, ' name ' => ' fj ', ' weight ' =>100, ' height ' =>180),
Array (' ID ' =>2, ' name ' => ' Tom ', ' Weight ' =>53, ' height ' =>150),
Array (' ID ' =>3, ' name ' => ' Jerry ', ' Weight ' =>120, ' height ' =>156),
Array (' ID ' =>4, ' name ' => ' Bill ', ' Weight ' =>110, ' height ' =>190),
Array (' ID ' =>5, ' name ' => ' linken ', ' weight ' =>80, ' height ' =>200),
Array (' ID ' =>6, ' name ' => ' Madana ', ' weight ' =>95, ' height ' =>110),
Array (' ID ' =>7, ' name ' => ' Jordan ', ' weight ' =>70, ' height ' =>170)
);
Var_dump ($person);
$person = My_sort ($person, ' name ', sort_asc,sort_string);
Var_dump ($person);
$person = My_sort ($person, ' weight ');
Var_dump ($person);
?>
The results are as follows:
Array (size=7)
0 =>
Array (size=4)
' ID ' => int 1
' Name ' => string ' fj ' (length=2)
' Weight ' => int 100
' Height ' => int 180
1 =>
Array (size=4)
' ID ' => int 2
' Name ' => string ' Tom ' (length=3)
' Weight ' => int 53
' Height ' => int 150
2 =>
Array (size=4)
' ID ' => int 3
' Name ' => string ' Jerry ' (length=5)
' Weight ' => int 120
' Height ' => int 156
3 =>
Array (size=4)
' ID ' => int 4
' Name ' => string ' Bill ' (length=4)
' Weight ' => int 110
' Height ' => int 190
4 =>
Array (size=4)
' ID ' => int 5
' Name ' => string ' linken ' (length=6)
' Weight ' => int 80
' Height ' => int 200
5 =>
Array (size=4)
' ID ' => int 6
' Name ' => string ' Madana ' (length=6)
' Weight ' => int 95
' Height ' => int 110
6 =>
Array (size=4)
' ID ' => int 7
' Name ' => string ' Jordan ' (length=6)
' Weight ' => int 70
' Height ' => int 170
Array (size=7)
0 =>
Array (size=4)
' ID ' => int 4
' Name ' => string ' Bill ' (length=4)
' Weight ' => int 110
' Height ' => int 190
1 =>
Array (size=4)
' ID ' => int 1
' Name ' => string ' fj ' (length=2)
' Weight ' => int 100
' Height ' => int 180
2 =>
Array (size=4)
' ID ' => int 3
' Name ' => string ' Jerry ' (length=5)
' Weight ' => int 120
' Height ' => int 156
3 =>
Array (size=4)
' ID ' => int 7
' Name ' => string ' Jordan ' (length=6)
' Weight ' => int 70
' Height ' => int 170
4 =>
Array (size=4)
' ID ' => int 5
' Name ' => string ' linken ' (length=6)
' Weight ' => int 80
' Height ' => int 200
5 =>
Array (size=4)
' ID ' => int 6
' Name ' => string ' Madana ' (length=6)
' Weight ' => int 95
' Height ' => int 110
6 =>
Array (size=4)
' ID ' => int 2
' Name ' => string ' Tom ' (length=3)
' Weight ' => int 53
' Height ' => int 150
Array (size=7)
0 =>
Array (size=4)
' ID ' => int 2
' Name ' => string ' Tom ' (length=3)
' Weight ' => int 53
' Height ' => int 150
1 =>
Array (size=4)
' ID ' => int 7
' Name ' => string ' Jordan ' (length=6)
' Weight ' => int 70
' Height ' => int 170
2 =>
Array (size=4)
' ID ' => int 5
' Name ' => string ' linken ' (length=6)
' Weight ' => int 80
' Height ' => int 200
3 =>
Array (size=4)
' ID ' => int 6
' Name ' => string ' Madana ' (length=6)
' Weight ' => int 95
' Height ' => int 110
4 =>
Array (size=4)
' ID ' => int 1
' Name ' => string ' fj ' (length=2)
' Weight ' => int 100
' Height ' => int 180
5 =>
Array (size=4)
' ID ' => int 4
' Name ' => string ' Bill ' (length=4)
' Weight ' => int 110
' Height ' => int 190
6 =>
Array (size=4)
' ID ' => int 3
' Name ' => string ' Jerry ' (length=5)
' Weight ' => int 120
' Height ' => int 156
The point here is to save the key to a one-dimensional array, and then use the Array_multisort () function to sort the array by key, and, of course, you can simply not apply the Array_multisort () function in this sort of order, This can be achieved only through a foreach traversal, but now that the PHP developer has provided us with a better solution, we can dispense with unnecessary trouble.