Get the App
SLTechnology News&Howtos  ›  Development  › 

Php usort tutorial on sorting values in a two-dimensional array using user-defined comparison functions

Shulou Source: shulou.com Published: 2022-06-03 09:11:12 10月02日 Update

This article introduces the "php usort uses user-defined comparison functions to sort values in two-dimensional array" related knowledge, in the actual case operation process, many people will encounter such a dilemma, then let Xiaobian lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!

For example, the following array:

[guess_subject] = > Array ([0] = > Array ([subject_id] = > 4587 [game_id] = > 9228 [parent_category_id] = > 78 [subject_title] = > the total score of the two teams in the game 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] = > whether there is overtime in this game [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] The sum of the total scores of the two teams in the second quarter 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] = > the total score of the two teams in the third quarter is [subject_date_time] = > 1490668200 [status] = > 2 [subject_answer] = > 0 [totle_join] = > 0 [max_nickname] = > [max_currency] = >))

To sort the status fields in ascending order, just write

Foreach ($data as $key = > & $value) {/ / sort the values in an array using a user-defined comparison function, and / / sort the values in an array with a user-defined comparison function. If the array you want to sort needs to be sorted by an unusual standard, you should 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 that is less than, equal to, or greater than 0 accordingly. Usort ($value ['guess_subject'], 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 of status=1 will come first.

This is the end of the tutorial on how php usort uses user-defined comparison functions to sort values in a two-dimensional array. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Functions arrays sorting users sum ascending order scores tutorials methods two-dimensional content parameters more knowledge games practical ordinary learned and then the whole audience Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Information Linux Docker Huawei