PHP two-dimensional array based on a specific sort of field implementation

Source: Internet
Author: User

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





*/

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.