- array_multisort: documentation ( source)
- var_export: documentation ( source)
- explode: documentation ( source)
<?php
$array = [
"GL001_M",
"GL001_XS",
"GL001_S",
"GL001_L",
"GL002_M",
"GL002_XS",
"GL002_S",
"GL002_L"
];
foreach ($array as $v) {
[$ids[], $sizes[]] = explode('_', $v, 2);
}
array_multisort(
$ids,
$sizes,
SORT_DESC,
$array
);
var_export($array);