Two-dimensional array sorting in PHP

Source: Internet
Author: User

Two-dimensional array sorting in PHP

For sorting two-dimensional arrays in PHP, you can use the PHP built-in function uasort ()

[Use the User-Defined comparison function to sort the values in the array and maintain the index Association]

The callback function is as follows: when the return value of the callback function is negative or false, it indicates that the first parameter of the callback function is placed before and the second parameter is arranged after.

 

$person = array(array('num'=>'001','id'=>6,'name'=>'zhangsan','age'=>21),array('num'=>'001','id'=>7,'name'=>'ahangsan','age'=>23),array('num'=>'003','id'=>1,'name'=>'bhangsan','age'=>23),array('num'=>'001','id'=>3,'name'=>'dhangsan','age'=>23),);

 

// Negative or false indicates that the first parameter should be in the previous function sort_by_name ($ x, $ y) {return strcasecmp ($ x ['name'], $ y ['name']);}
Use:

 

 

uasort($person,'sort_by_name');

 

The following provides a two-dimensional array sorting method for reference and interview:

 

// $ Array the array to be sorted // $ row sort by column // $ type [asc or desc] // return the sorted array function array_sort ($ array, $ row, $ type) {$ array_temp = array (); foreach ($ array as $ v) {$ array_temp [$ v [$ row] = $ v ;} if ($ type = 'asc ') {ksort ($ array_temp);} elseif ($ type = 'desc') {krsort ($ array_temp );} else {} return $ array_temp ;}

 

========================================================== ==================================

By the way, several PHP sorting functions

Sort arrays in sort mode. This mode is applicable to one-dimensional index arrays without indexes.

Rsort sorts arrays reversely.

 

[Asort sorts arrays and maintains the index relationship] sorts values, which is generally applicable to one-dimensional arrays and maintains the index relationship.

[Arsort sorts arrays in reverse order and maintains the index relationship] is consistent with the asort usage.

 

[Ksort sorts arrays by key names]

[Krsort sorts arrays in reverse order by key name]

========================================================== ==================================

 

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.