An array arrives with some or all of the following values, in any order. What's the best way to order them in ascending size order? So starting with small and ending with XXL. I can usort but am a bit lost as to how the elements should be ordered in my user defined function
Small
XXL
Medium
Large
XL
EDIT: left out some info so created new question http://stackoverflow.com/questions/4014743/custom-ordering-array-with-key-value-pairs
EDIT2: Full code
print_r($sizes);
$sorted_sizes = $this->sort_sizes(array_unique($sizes));
print_r($sorted_sizes);
function sort_sizes($sizes)
{
return uasort($sizes, array($this, 'cmp'));
}
function cmp($a,$b)
{
$sizeArray = array( 'Small' => 0, 'Medium' => 1, 'Large' => 2, 'XL' => 3, 'XXL' => 4);
return $sizeArray[$a] - $sizeArray[$b];
}
This outputs:
Array
(
[66-507cddcd16d9786abafccfa78b19acf8] => XL
[64-507cddcd16d9786abafccfa78b19acf8] => medium
[65-507cddcd16d9786abafccfa78b19acf8] => large
[63-507cddcd16d9786abafccfa78b19acf8] => small
)
and print_r($sorted_sizes) just gives output "1"