Last active
February 16, 2020 18:34
-
-
Save lincoln-chawora/a92ac4259e395f45c24ed2e75e74fe46 to your computer and use it in GitHub Desktop.
How to sort an array by a value inside the array in php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
uasort($valid_variants_ids, function ($a, $b) { | |
// if the first item and the second item have the same value, do nothing. | |
if ($a['study_option_weight'] == $b['study_option_weight']) { | |
return 0; | |
} | |
// if the first item is less than the second item, move the first option down by one, otherwise move it up by one. | |
return $a['study_option_weight'] < $b['study_option_weight'] ? -1 : 1; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array | |
( | |
[3] => Array | |
( | |
[id] => 2140 | |
[revision_id] => 285900 | |
[start_year_month_weight] => 15 | |
[study_option_weight] => 0 | |
) | |
[8] => Array | |
( | |
[id] => 2985 | |
[revision_id] => 285911 | |
[start_year_month_weight] => 15 | |
[study_option_weight] => 1 | |
) | |
[9] => Array | |
( | |
[id] => 2987 | |
[revision_id] => 285914 | |
[start_year_month_weight] => 16 | |
[study_option_weight] => 3 | |
) | |
[10] => Array | |
( | |
[id] => 2989 | |
[revision_id] => 285917 | |
[start_year_month_weight] => 16 | |
[study_option_weight] => 0 | |
) | |
[12] => Array | |
( | |
[id] => 13438 | |
[revision_id] => 285923 | |
[start_year_month_weight] => 16 | |
[study_option_weight] => 4 | |
) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array | |
( | |
[3] => Array | |
( | |
[id] => 2140 | |
[revision_id] => 285900 | |
[start_year_month_weight] => 15 | |
[study_option_weight] => 0 | |
) | |
[10] => Array | |
( | |
[id] => 2989 | |
[revision_id] => 285917 | |
[start_year_month_weight] => 16 | |
[study_option_weight] => 0 | |
) | |
[8] => Array | |
( | |
[id] => 2985 | |
[revision_id] => 285911 | |
[start_year_month_weight] => 15 | |
[study_option_weight] => 1 | |
) | |
[9] => Array | |
( | |
[id] => 2987 | |
[revision_id] => 285914 | |
[start_year_month_weight] => 16 | |
[study_option_weight] => 3 | |
) | |
[12] => Array | |
( | |
[id] => 13438 | |
[revision_id] => 285923 | |
[start_year_month_weight] => 16 | |
[study_option_weight] => 4 | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment