I am currently trying to sort some clothing size arrays (S M L XL XXL etc.) that belong to an array. I am able to do this via the following function (thanks to this place Php Array Sorting Clothing Sizes (XXS XS S M L XL XXL) and Numbers on a Dynamic Array):
function cmp($a, $b) {
$sizes = array(
"XXS" => 0,
"XS" => 1,
"S" => 2,
"M" => 3,
"L" => 4,
"XL" => 5,
"XXL" => 6
);
$asize = $sizes[$a];
$bsize = $sizes[$b];
if ($asize == $bsize) {
return 0;
}
return ($asize > $bsize) ? 1 : -1;
}
usort($the_array, "cmp");
This is all very well for an array that looks like this: $the_array("S", "M", "XL"). However, my array looks a bit like this:
$the_array = array("S : price £10", "XXL : price £10", "M : price £10", "XS : price £10")
This makes it not work... I need a function that ideally only looks at the first part of the array up to the ":". Is there such a thing?
Thanks for the help.