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