You just need to write custom string parts comparison function for usort()
:
$products = [
"101.6mm x 101.6mm",
"50.8mm x 50.8mm",
"76.2mm x 76.2mm",
"100mm x 100mm",
"15mm x 15mm",
"20mm x 20mm",
"50mm x 50.2mm",
"25mm x 25mm",
"30mm x 45mm",
"30mm x 30mm",
"40mm x 40mm",
"50mm x 50mm",
"60mm x 60m",
"80mm x 80mm",
];
usort($products,
function($a, $b) {
list($vals_a, $vals_b) = [explode('x', $a), explode('x', $b)];
$vals_a[0] = preg_replace('/(\s|mm)/mui', '',$vals_a[0]);
$vals_a[1] = preg_replace('/(\s|mm)/mui', '',$vals_a[1]);
$vals_b[0] = preg_replace('/(\s|mm)/mui', '',$vals_b[0]);
$vals_b[1] = preg_replace('/(\s|mm)/mui', '',$vals_b[1]);
if ($vals_a[0] < $vals_b[0])
return -1;
elseif ($vals_a[0] > $vals_b[0])
return +1;
else {
if ($vals_a[1] < $vals_b[1])
return -1;
elseif ($vals_a[1] > $vals_b[1])
return +1;
else return 0;
}
});
var_dump($products);