The functionality that is documented in this article is similar to the order by in MySQL, where a requirement is encountered in the previous project.
requirements: Get each of the 4 data from two different tables, then consolidate (Array_merge) into an array, and then sort the top 4 by descending the time the data was created.
When you meet this requirement, it is not a problem that order by can solve. So look through the PHP manual to find the following methods to do this note.
less nonsense, the code, the list is as follows:
. The code is as follows:
<?php
/**
* Two-dimensional arrays are sorted according to a field
* Function: According to the user's age reverse order
* @author ruxing.li
*/
header (' content-type:text/html; Charset=utf-8 ');
$arrUsers = Array (
Array (
' id ' => 1,
' name ' => ' John ',
' age ' => 25,
),
Array (
' id ' => 2,
' name ' => ' Dick ',
' age ' => 23,
),
Array (
' id ' => 3,
' name ' => ' Harry ',
' age ' => 40,
),
Array (
' ID ' => 4,
' name ' => ' Zhao Liu ',
' age ' => 31,
),
Array (
' id ' => 5,
' name ' => ' Yellow Seven ',
' age ' => 20,
),
);
$sort = Array (
' direction ' => ' sort_desc ',//Sort order flag Sort_desc descending; SORT_ASC Ascending
' field ' => ' age ',//Sort field
);
$arrSort = Array ();
foreach ($arrUsers as $uniqid => $row) {
foreach ($row as $key => $value) {
$arrSort [$key] [$uniqid] = $value;
}
}
if ($sort [' direction ']) {
Array_multisort ($arrSort [$sort [' field ']], constant ($sort [' direction ']), $arrUsers);
}
Var_dump ($arrUsers);
/*
Output Result:
Array (size=5)
0 =>
Array (size=3)
' id ' => int 5
' name ' => string ' Yellow Seven ' (length=6)
' age ' => int 20
1 =>
Array (size=3)
' id ' => int 2
' name ' => string ' Dick ' (length=6)
' age ' => int 23
2 =>
Array (size=3)
' id ' => int 1
' name ' => string ' John ' (length=6)
' age ' => int 25
3 =>
Array (size=3)
' id ' => int 4
' name ' => string ' Zhao Liu ' (length=6)
' age ' => int 31
4 =>
Array (size=3)
' id ' => int 3
' name ' => string ' Harry ' (length=6)
' age ' => int 40
*/