No matter how you decide to isolate the substring that follows R
, you should try to minimize the number of times that function calls are made.
Using array_multisort()
with mapped calls of the isolating technique will perform better than a usort()
approach because usort()
will perform the isolating technique multiple times on the same value.
In my snippet below, I do not remove the R
while isolating, so I use natural sorting to have the numeric substrings compared correctly. If you aren't familiar with strpbrk()
then see this answer for a demonstration.
Code: (Demo)
$array = [
'215/75R17.5',
'235/75R17.5',
'8.25R16',
'7.00R16',
'11R22.5',
'7.50R16'
];
array_multisort(array_map(fn($v) => strpbrk($v, 'R'), $array), SORT_NATURAL, $array);
var_export($array);
An approach that purely isolates the numeric values will not need to sort naturally. (Demo)
array_multisort(array_map(fn($v) => sscanf($v, '%*[^R]R%f')[0], $array), $array);
var_export($array);