Sort a set of multidimensional arrays by array elements
Do you want to order them by the value of the "int" key ?
Use uasort with a callback function :
function compare_by_int_key($a, $b) {
if ($a['int'] == $b['int']) {
return 0;
}
return ($a['int'] < $b['int']) ? -1 : 1;
}
uasort($arr, "compare_by_int_key");
PHP has come along way since 2010. From PHP7, the spaceship operator took a lot of convolution out of three-way sorting.
usort($arr, function($a, $b){ return $a['int'] <=> $b['int']; });
From PHP7.4, "arrow function" syntax removed some bloat from the process.
usort($arr, fn($a, $b) => $a['int'] <=> $b['int']);
Built into the magic of the spaceship operator is the fact that it will compare numeric strings as numeric type values and compare strings as strings.
Further awesomeness includes the fact that if you want to state multiple sorting rules, you only need to build a balanced array on both sides of the operator.
// sort by "int" values ASC, then "a" values ASC
usort($arr, fn($a, $b) => [$a['int'], $a['a']] <=> [$b['int'], $b['a']]);
Online demos of above: https://3v4l.org/QjNg7