Here's a custom comparison function that uses a predefined order list.
function cmp($a,$b) {
$order = array_flip(
array(
'white',
'blue',
'purple',
'brown',
'black',
'black-red',
'coral',
'white-red',
'red',
)
);
$a = strtolower($a);
$b = strtolower($b);
if(!isset($order[$a])) {
if(!isset($order[$b])) {
return strcmp($a,$b);
}
return 1;
}
if(!isset($order[$b])) {
return -1;
}
return $order[$a] - $order[$b];
}
Any 2 words that exist on the list are copared according to their order.
If 1 word exists on the list and another doesn't, the latter is "lower".
If both don't exist on the list, they're copared like any other pair of strings.
Use the function with usort
like this:
$colors = array(
'blue',
'purple',
'white',
'foo',
'bar',
);
usort($colors,'cmp');
print_r($colors);
You'll get:
Array
(
[0] => white
[1] => blue
[2] => purple
[3] => bar
[4] => foo
)