usort allows custom inline functions and variables to be used with them:
eg
usort($arr, function($a, $b) use ($key) {
return strcmp($a[$key], $b[$key]);
});
usort also allows the sort functions to be defined separately
eg
function cmp($a, $b) {
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
usort($arr, 'cmp');
I have a large comparison function that I want to define separately and not as an inline function. How do I pass a variable to usort and cmp?