Argh...I swear I've read about 20 articles on usort, but can't find the answer to what must be a common dilemma:
I have an array that is formatted as such:
And what I want to do is give the user some radio buttons so that they can pick which field to sort by, and also whether to do it ascending or descending. I understand how usort works, but in order to use it, I have to do something like:
How do I work with usort so I can enter in other variables that dictate by which attribute the array is sorted by, so I don't have to write a bunch of comparison functions and make different calls to usort?
I have an array that is formatted as such:
Code:
Array
(
[0] => Array
(
[title] => Dog
[2000] => 278
[2007] => 50
)
[1] => Array
(
[title] => Cat
[2000] => 41
[2007] => 740
)
[2] => Array
(
[title] => Monkey
[2000] => 378
[2007] => 65
)
)
And what I want to do is give the user some radio buttons so that they can pick which field to sort by, and also whether to do it ascending or descending. I understand how usort works, but in order to use it, I have to do something like:
Code:
function compar($a, $b) {
return strnatcmp($a["title"], $b["title"]);
}
usort($array, 'compar');
How do I work with usort so I can enter in other variables that dictate by which attribute the array is sorted by, so I don't have to write a bunch of comparison functions and make different calls to usort?