I have an array:
$a = array("Freshman Spring" => 3, "Freshman Winter" => 2, "Freshman Summer" => 4, "Freshman Fall" => 1);
There will be different arrays for each school year, but no array will mix years such as fre, soph.
My issue is that I need to sort this array by the way seasons happen in school (Fall, Winter, Spring, Summer). I tried using uksort but my code there actually accomplishes nothing since it’d sort the same way regardless.
function cmp($a, $b) {
$a = preg_replace('@^(freshman|sophomore|junior|senior) @', '', $a);
$b = preg_replace('@^(freshman|sophomore|junior|senior) @', '', $b);
return strcasecmp($a, $b);
}
uksort($a, "cmp");
I get this:
Array
(
[Freshman Fall] => 1
[Freshman Spring] => 3
[Freshman Summer] => 4
[Freshman Winter] => 2
)
but need to get this:
Array
(
[Freshman Fall] => 1
[Freshman Winter] => 2
[Freshman Spring] => 3
[Freshman Summer] => 4
)
Is uksort the way to go? and what am I doing wrong? Keep in mind that the array values will not be those values, they will hold objects. Any and all help is greatly appreciated!
Why not use the value as the key {1,2,3,4} and store the “object” as the value. Makes sorting really easy…
You can always map the {1,2,3,4} to defines or class consts if you want pretty names.