PHP按文件名中的数字对数组排序

PHP按文件名中的数字对数组排序,php,html,arrays,sorting,Php,Html,Arrays,Sorting,我正在开发一个照片库,它根据文件名的编号自动对照片进行排序 我有以下代码: //calculate and sort $totaal = 0; if($handle_thumbs = opendir('thumbs')){ $files_thumbs = array(); while(false !== ($file = readdir($handle_thumbs))){ if($file != "." && $file != ".."){

我正在开发一个照片库,它根据文件名的编号自动对照片进行排序

我有以下代码:

//calculate and sort 
$totaal = 0;
if($handle_thumbs = opendir('thumbs')){
    $files_thumbs = array();
    while(false !== ($file = readdir($handle_thumbs))){
        if($file != "." && $file != ".."){
            $files_thumbs[] = $file;
            $totaal++;
        }
    }
    closedir($handle_thumbs);
}
sort($files_thumbs);


//reset array list
$first = reset($files_thumbs);
$last = end($files_thumbs);
//match and split filenames from array values - image numbers
preg_match("/(\d+(?:-\d+)*)/", "$first", $matches);
$firstimage = $matches[1];
preg_match("/(\d+(?:-\d+)*)/", "$last", $matches);
$lastimage = $matches[1];
但是当我有像
photo-Aname\u 0333.jpg
photo-Bname\u 0222.jpg
这样的文件名时,它确实是以
photo-Aname\u 0333
而不是
0222
开头的


如何按文件名编号排序?

它按字母顺序排序,因为您在文件名上使用了sort()。代码的第二部分不起任何作用

你可能想看看usort 你可以这样做

function cmp($a, $b) {
    if ($a == $b) {
        return 0;
    }
    preg_match('/(\d+)\.\w+$/', $a, $matches);
    $nrA = $matches[1];
    preg_match('/(\d+)\.\w+$/', $b, $matches);
    $nrB = $matches[1];

    return ($nrA < $nrB) ? -1 : 1;
}

usort($files_thumb, 'cmp');
函数cmp($a,$b){
如果($a=$b){
返回0;
}
预匹配(“/(\d+)\。\w+$/”,$a,$matches);
$nrA=$matches[1];
预匹配(“/(\d+)\。\w+$/”,$b,$matches);
$nrB=$matches[1];
回报率($nrA<$nrB)?-1:1;
}
usort($files_thumb,'cmp');

我也不确定你的正则表达式,考虑一个名为“ABC1234 CDE23 45 XX”的文件。我使用的是文件扩展名末尾前的最后一个数字。但这一切都取决于您的文件名。

它按字母顺序排序,因为您对文件名使用sort()。代码的第二部分不起任何作用

sort(array,sortingtype) , you have to set the second parameter of the sort() function to 1 so it will sort items numerically 



 //calculate and sort 
$totaal = 0;
if($handle_thumbs = opendir('thumbs')){
    $files_thumbs = array();
    while(false !== ($file = readdir($handle_thumbs))){
        if($file != "." && $file != ".."){
            $files_thumbs[] = $file;
            $totaal++;
        }
    }
    closedir($handle_thumbs);
}
sort($files_thumbs,1);
//reset array list
$first = reset($files_thumbs);
$last = end($files_thumbs);
//match and split filenames from array values - image numbers
preg_match("/(\d+(?:-\d+)*)/", "$first", $matches);
$firstimage = $matches[1];
preg_match("/(\d+(?:-\d+)*)/", "$last", $matches);
$lastimage = $matches[1];
你可能想看看usort 你可以这样做

function cmp($a, $b) {
    if ($a == $b) {
        return 0;
    }
    preg_match('/(\d+)\.\w+$/', $a, $matches);
    $nrA = $matches[1];
    preg_match('/(\d+)\.\w+$/', $b, $matches);
    $nrB = $matches[1];

    return ($nrA < $nrB) ? -1 : 1;
}

usort($files_thumb, 'cmp');
函数cmp($a,$b){
如果($a=$b){
返回0;
}
预匹配(“/(\d+)\。\w+$/”,$a,$matches);
$nrA=$matches[1];
预匹配(“/(\d+)\。\w+$/”,$b,$matches);
$nrB=$matches[1];
回报率($nrA<$nrB)?-1:1;
}
usort($files_thumb,'cmp');

我也不确定你的正则表达式,考虑一个名为“ABC1234 CDE23 45 XX”的文件。我使用的是文件扩展名末尾前的最后一个数字。但这完全取决于您的文件名。

usort是一个php函数,用于使用值对数组进行排序。
sort(array,sortingtype) , you have to set the second parameter of the sort() function to 1 so it will sort items numerically 



 //calculate and sort 
$totaal = 0;
if($handle_thumbs = opendir('thumbs')){
    $files_thumbs = array();
    while(false !== ($file = readdir($handle_thumbs))){
        if($file != "." && $file != ".."){
            $files_thumbs[] = $file;
            $totaal++;
        }
    }
    closedir($handle_thumbs);
}
sort($files_thumbs,1);
//reset array list
$first = reset($files_thumbs);
$last = end($files_thumbs);
//match and split filenames from array values - image numbers
preg_match("/(\d+(?:-\d+)*)/", "$first", $matches);
$firstimage = $matches[1];
preg_match("/(\d+(?:-\d+)*)/", "$last", $matches);
$lastimage = $matches[1];
usort需要一个接收2个值的回调函数

在回调中,根据需要,将返回比较结果1、0或-1。例如,为了对数组asc进行排序,当回调的firts值小于second值时,我返回-1

在这种特殊情况下,我获取文件名的编号,并将其作为字符串进行比较,不必强制转换为整数

<?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);

usort是一个php函数,用于使用值对数组进行排序。 usort需要一个接收2个值的回调函数

在回调中,根据需要,将返回比较结果1、0或-1。例如,为了对数组asc进行排序,当回调的firts值小于second值时,我返回-1

在这种特殊情况下,我获取文件名的编号,并将其作为字符串进行比较,不必强制转换为整数

<?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);

之前的答案都没有使用最合适的/现代的技术来进行三方比较--宇宙飞船操作员(

它不仅提供了更整洁的语法,还允许您在一个步骤中实现多个排序规则

下面的代码段将每个文件名字符串一分为二(在下划线上),然后首先比较两个文件名的下半部,如果下半部有平局,则将比较两个文件名的上半部

sort(array,sortingtype) , you have to set the second parameter of the sort() function to 1 so it will sort items numerically 



 //calculate and sort 
$totaal = 0;
if($handle_thumbs = opendir('thumbs')){
    $files_thumbs = array();
    while(false !== ($file = readdir($handle_thumbs))){
        if($file != "." && $file != ".."){
            $files_thumbs[] = $file;
            $totaal++;
        }
    }
    closedir($handle_thumbs);
}
sort($files_thumbs,1);
//reset array list
$first = reset($files_thumbs);
$last = end($files_thumbs);
//match and split filenames from array values - image numbers
preg_match("/(\d+(?:-\d+)*)/", "$first", $matches);
$firstimage = $matches[1];
preg_match("/(\d+(?:-\d+)*)/", "$last", $matches);
$lastimage = $matches[1];
代码:()


对于那些仍然认为
preg\uu
调用更好的人,我将解释我的代码片段可能进行了两次比较,而
preg\u
解决方案只进行了一次比较

如果您只希望使用一个排序标准,则此非正则表达式技术将优于正则表达式:

usort($photos, function ($a, $b) {
   return strstr($a, '_') <=> strstr($b, '_');
});
usort($photos,function($a,$b){
返回strstr($a,'''.'str($b,''.');
});

我非常喜欢正则表达式,但我只知道在非正则表达式技术无法提供有价值的优势时才使用它。

之前的答案都没有使用最合适的/现代的技术来进行三方比较--宇宙飞船操作员(

它不仅提供了更整洁的语法,还允许您在一个步骤中实现多个排序规则

下面的代码段将每个文件名字符串一分为二(在下划线上),然后首先比较两个文件名的下半部,如果下半部有平局,则将比较两个文件名的上半部

sort(array,sortingtype) , you have to set the second parameter of the sort() function to 1 so it will sort items numerically 



 //calculate and sort 
$totaal = 0;
if($handle_thumbs = opendir('thumbs')){
    $files_thumbs = array();
    while(false !== ($file = readdir($handle_thumbs))){
        if($file != "." && $file != ".."){
            $files_thumbs[] = $file;
            $totaal++;
        }
    }
    closedir($handle_thumbs);
}
sort($files_thumbs,1);
//reset array list
$first = reset($files_thumbs);
$last = end($files_thumbs);
//match and split filenames from array values - image numbers
preg_match("/(\d+(?:-\d+)*)/", "$first", $matches);
$firstimage = $matches[1];
preg_match("/(\d+(?:-\d+)*)/", "$last", $matches);
$lastimage = $matches[1];
代码:()


对于那些仍然认为
preg\uu
调用更好的人,我将解释我的代码片段可能进行了两次比较,而
preg\u
解决方案只进行了一次比较

如果您只希望使用一个排序标准,则此非正则表达式技术将优于正则表达式:

usort($photos, function ($a, $b) {
   return strstr($a, '_') <=> strstr($b, '_');
});
usort($photos,function($a,$b){
返回strstr($a,'''.'str($b,''.');
});

我非常喜欢正则表达式,但我知道只有在非正则表达式技术无法提供有价值的优势时才使用它。

但是如何在文件夹中包含100多个文件时使用它。我忘了,在preg_匹配之后,我有://set imagenr if(!isset($\u GET[“image”]){$imagenr=$firstimage;}否则{$imagenr=$\u GET[“image”];}$firstimage应该匹配文件夹中的最低数字,$lastimage应该匹配最新数字。所以我想我可以使用preg_匹配从文件名中分割数字,从第一个数字开始计数,直到最新可用的$files_thumb是一个包含所有100多个文件名的数组,您在while循环中收集它们。usort接受此数组并应用自定义排序函数。因此,在$files\u thumb之后,仍然是包含所有文件的数组,只是按文件编号排序。但是如何处理文件夹中的100多个文件呢。我忘了,在preg_匹配之后,我有://set imagenr if(!isset($\u GET[“image”]){$imagenr=$firstimage;}否则{$imagenr=$\u GET[“image”];}$firstimage应该匹配文件夹中的最低数字,$lastimage应该匹配最新数字。所以我想我可以使用preg_匹配从文件名中分割数字,并从第一个数字开始计数,直到最新的可用$files