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:
Copy Code code as follows:
<?php
/**
* Two-dimensional arrays are sorted by a field
* Function: According to the user's age in 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 results:
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
*/