Php usort uses a user-defined comparison function to sort values in two-dimensional arrays.

Source: Internet
Author: User

Php usort uses a user-defined comparison function to sort values in two-dimensional arrays.

Today, we found a php method that uses 2D array sorting, usort, which is recommended to everyone. In the next two-dimensional array, sort by the value of a field. This method is simple and efficient, for example, the following array:

[Guess_subject] => Array ([0] => Array ([subject_id] => 4587 [game_id] => 9228 [parent_category_id] => 78 [subject_title] => two matches the sum of the team's total score is [subject_date_time] => 1490668200 [status] => 2 [subject_answer] => 0 [totle_join] => 0 [max_nickname] => [max_currency] =>) [1] => Array ([subject_id] => 4588 [game_id] => 9228 [parent_category_id] => 78 [subject_title] => is there any overtime match in this competition [subject_date_time] => 1490668200 [status] => 2 [subject_answer] => 0 [totle_join] => 0 [max_nickname] => [max_currency] =>) [2] => Array ([subject_id] => 4584 [game_id] => 9228 [parent_category_id] => 78 [subject_title] => section 2. The sum of the total score of the two teams is [subject_date_time] => 1490668200 [status] => 1 [subject_answer] => 0 [totle_join] => 0 [max_nickname] => [max_currency] =>) [3] => Array ([subject_id] => 4585 [game_id] => 9228 [parent_category_id] => 78 [subject_title] => section 3, the sum of the total score of the two teams is [subject_date_time] => 1490668200 [status] => 2 [subject_answer] => 0 [totle_join] => 0 [max_nickname] => [max_currency] => ))

To sort the status field in ascending order, you only need to write

Foreach ($ data as $ key =>&$ value) {// sort the values in the array in ascending order by status using the User-Defined comparison function, // This function uses the User-Defined comparison function to sort the values in an array. If the array to be sorted needs to be sorted by an unusual standard, use this function. // When the first parameter is less than, equal to, or greater than the second parameter, the comparison function must return an integer less than, equal to, or greater than 0. Usort ($ value ['Guess _ subobject'], function ($ a, $ B) {if ($ a ['status'] = $ B ['status']) return 0; return ($ a ['status'] <$ B ['status'])? -1: 1 ;}) ;}return $ data;

It will be sorted in ascending order of status, and the array units with status = 1 will be placed in the front

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.