<?php
function sort_array_item_by_size($a, $b): int
{
static $sizes = [
"XXS" => 0,
"XS" => 1,
"S" => 2,
"M" => 3,
"L" => 4,
"XL" => 5,
"XXL" => 6,
];
// The next three lines really should include some error checking
$size_a = explode("_", $a)[1];
$size_b = explode("_", $b)[1];
return $sizes[$size_a] <=> $sizes[$size_b];
}
$your_array = ["GL001_XXL", "GL001_L", "GL001_XXS", "GL001_S"];
usort($your_array, "sort_array_item_by_size");
print_r($your_array);
preferences:
28.99 ms | 406 KiB | 5 Q