The two-dimensional PHP array sorts Fields. the functions recorded in this article are similar to the order by function in MySQL. this requirement was encountered 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:
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', // 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 7' (length = 6) 'age' => int 20 1 => array (size = 3) 'id' => int 2 'name' => string 'Lily' (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 '王' (length = 6) 'age' => int 40 */