2017-06-20 22 views
0

我正在基于文件名的编号自动对照片进行排序的照片库。通过文件名中的数字排序PHP数组

我有以下代码:

//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_0333.jpgphoto-Bname_0222.jpg,它确实与photo-Aname_0333而不是0222开始。

我怎样才能按文件名数字排序?

回答

1

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

在回调中,根据您的需要,您将返回比较结果1,0或-1。例如,要排序数组asc,当回调的firts值小于第二个值时,我返回-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); 
+0

这个工作正常,但你能解释一下吗? :) – Rick

+0

我补充说明里克 – David

0

它按字母顺序排序,因为您在文件名上使用sort()。你的代码的第二部分什么都不做。

你可能想看看usort http://php.net/manual/en/function.usort.php 你可以做这样的事情

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

此外,我不知道您正则表达式,考虑一个名为“abc1234cde2345xx”文件。我用过的最后一个数字在文件扩展名前面。但这一切都取决于你的文件名。

+0

但是如何在一个文件夹中使用100多个文件来做到这一点。 我忘了,的preg_match后我有: //设置imagenr 如果(!isset($ _ GET [ “形象”])){$ imagenr = $ firstimage;} 其他{$ imagenr = $ _GET [” image;];} $ firstimage应该匹配文件夹中最低的数字,$ lastimage最新。所以我想我可以使用preg_match从文件名拆分数字,并从第一个数字开始计数,直到最新的可用 – Rick

+0

$ files_thumb是一个包含所有100个文件名的数组,您在while循环中收集它们。 usort使用此数组并应用自定义排序功能。所以在这之后$ files_thumb仍然是包含所有文件的数组,只是按文件号排序。 – masterfloda

0
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];