Two examples of php 2D array sorting-php Tutorial

Source: Internet
Author: User
Two examples of php 2D array sorting
This article introduces two examples of two-dimensional array sorting in php, which are relatively simple. if you are learning php array sorting, you can make a reference. it is also good.

Share a piece of code to sort two-dimensional arrays in php. Code:

     '; Print_r ($ good); echo'
'; $ Hot = array (); $ follow = array (); foreach ($ good as $ k => $ v) {$ hot [$ k] = $ v ['Hot ']; $ follow [$ k] = $ v ['follow'];} // sort two-dimensional arrays // Method 1: Sort by hot fields in descending order, and then array_multisort ($ hot, SORT_DESC, $ follow, SORT_DESC, $ good); echo'
';print_r($good);echo '
'; // Method 2 function xx ($ a, $ B) {if ($ a ['price'] = $ B ['price']) {return ($ a ['Hot '] <$ B ['Hot'])? 1:-1;} return ($ a ['price'] <$ B ['price'])? 1:-1 ;}$ a = array (0 => array ('price' => 123, 'Hot '=> 34543 ), 1 => array ('price' => 434, 'Hot '=> 234), 2 => array ('price' => 42, 'Hot' => 2232 ), 3 => array ('price' => 42, 'Hot '=> 235432), 4 => array ('price' => 33443, 'Hot' => 12 ), 4 => array ('price' => 434, 'Hot '=> 1211),); usort ($ a, 'XX'); echo'
';print_r($a);echo '
';

>>> For more information, see The php array sorting method <

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.