usort is a php function to sort array using values.
usort needs a callback function that receives 2 values.
In the callback, depending of your needs, you will be return the result of the comparision 1, 0 or -1. For example to sort the array asc, I return -1 when the firts value of the callback is less than second value.
In this particular case I obtain the numbers of the filename, and compare it as string, is not necesary to cast as integer.
<?php
$photos=[
'photo-Bname_0222.jpg',
'photo-Aname_0333.jpg',
'photo-Cname_0111.jpg',
];
usort($photos, function ($a, $b) {
preg_match("/(d+(?:-d+)*)/", $a, $matches);
$firstimage = $matches[1];
preg_match("/(d+(?:-d+)*)/", $b, $matches);
$lastimage = $matches[1];
if ($firstimage == $lastimage) {
return 0;
}
return ($firstimage < $lastimage) ? -1 : 1;
});
print_r($photos);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…