Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to sort an array by a value inside the array in php
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;
});
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
)
)
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