Obtain the four data records from two different tables, and then combine them into an array. Then, sort the first four data records in descending order based on the data creation time.
Obtain the four data records from two different tables, and then combine them into an array. Then, sort the first four data records in descending order based on the data creation time.
The functions to be implemented in this document are similar to order by in MySQL. This is a requirement in the previous project.
Requirement: Obtain the four data records from two different tables, then combine (array_merge) into an array, and then sort the first four data records in descending order based on the data creation time.
This requirement is not a problem that can be solved by order. So I found the following method in the PHP manual and took this note.
The following code is provided:
The Code is as follows:
/**
* Two-dimensional arrays are sorted by a field.
* Function: sort by user's age in descending order
* @ Author ruxing. li
*/
Header ('content-Type: text/html; Charset = UTF-8 ');
$ ArrUsers = array (
Array (
'Id' => 1,
'Name' => 'zhang san ',
'Age' => 25,
),
Array (
'Id' => 2,
'Name' => 'Li si ',
'Age' => 23,
),
Array (
'Id' => 3,
'Name' => 'wang 5 ',
'Age' => 40,
),
Array (
'Id' => 4,
'Name' => 'zhao liu ',
'Age' => 31,
),
Array (
'Id' => 5,
'Name' => 'yellow 7 ',
'Age' => 20,
),
);
$ Sort = array (
'Direction' => 'sort _ desc', // SORT order marker SORT_DESC in descending order; SORT_ASC in ascending order
'Field' => 'age', // sorting 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 7' (length = 6)
'Age' => int 20
1 =>
Array (size = 3)
'Id' => int 2
'Name' => string 'Li si' (length = 6)
'Age' => int 23
2 =>
Array (size = 3)
'Id' => int 1
'Name' => string 'zhang san' (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 'wang 5' (length = 6)
'Age' => int 40
*/