Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report