PHP implementation of sorting two-dimensional arrays by a field _ PHP Tutorial

Source: Internet
Author: User
PHP two-dimensional array sorting based on a specific field implementation. The functions to be implemented in this document are similar to ORDERBY in MySQL. this is a requirement in the previous project. Requirement: obtain the four data records from two different tables. the functions recorded in this article are similar to order by in MySQL. this requirement is met 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

*/

Sort order by in MySQL, which was encountered in the previous project. Requirement: obtain the four data records from two different tables, and then...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.