PHP Two-dimensional arrays are sorted according to a field

Source: Internet
Author: User

<?php/** * Two-dimensional array sorted by a field * function: sorted by user's age in reverse order * @author Ruxing.li */Header (' content-type:text/html;  Charset=utf-8 ');      $arrUsers = array (' id ' = = 1, ' name ' = ' Zhang San ', ' age ' = 25,      ), array (' id ' = = 2, ' name ' = ' John Doe ', ' age ' = 23,),              Array (' id ' = = 3, ' name ' = ' Harry ', ' age ' = +,), Array (              ' id ' = 4, ' name ' = ' Zhao Liu ', ' age ' = ', ', 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: Array (size=5) 0 = = Array (size=3) ' id ' + = int 5 ' name ' = = String ' Yellow Seven ' (length=6)       ' Age ' = int 1 = array (size=3) ' id ' + = int 2 ' name ' = = String ' John Doe ' (length=6)       ' Age ' = int 2 = array (size=3) ' id ' + = int 1 ' name ' = = String ' Zhang San ' (length=6) ' Age ' = int 3 = array (size=3) ' id ' + = int 4 ' name ' = = String ' Zhao Liu ' (length=6) ' A GE ' = = int 4 + = array (size=3) ' id ' + = int 3 ' name ' = = String ' Harry ' (length=6) ' Age   ' = = Int 40 */

Reproduced in: http://blog.csdn.net/liruxing1715/article/details/28265495

PHP Two-dimensional arrays are sorted according to a field

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.