3v4l.org

run code in 300+ PHP versions simultaneously
<?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);
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
Array ( [0] => GL001_XXS [1] => GL001_S [2] => GL001_L [3] => GL001_XXL )

preferences:
84.14 ms | 406 KiB | 5 Q