PHP glob(*) 和 foreach by largest number basename
PHP glob(*) and foreach by largest number basename
我有这个文件路径/0.php路径/3.php路径/2.php路径/7.php
我想用 foreach 显示文件,我使用了这个代码:
$files = glob("system/$var/$var/$var/*/*.php");
foreach($files as $file) {
//code here per $file.
//for example:
$basename = basename($file);
echo str_replace(".php",null,$basename) . "<br>";
include $file; //heres i need the path of the files.
}
但我想按 .php 扩展名之前的最大数字对这些文件进行排序,如下所示:
Array
(
[0] => path/7.php
[1] => path/3.php
[2] => path/2.php
[3] => path/0.php
)
所以?
更新,rsort 不适合我,因为它是 path/to/file,在数字之前,我想使用文件的路径。
$files = glob("system/$var/$var/$var/*/*.php");
//Put each file name into an array called $filenames
foreach($files as $i => $file) $filenames[$i] = basename($file);
//Sort $filenames
rsort($filenames);
//Check output
print_r($filenames);
这将输出:
Array
(
[0] => 7.php
[1] => 3.php
[2] => 2.php
[3] => 0.php
)
要保留完整路径,您可以使用路径作为键,使用基本名称作为值:
#For testing:
#$files = array("path/3.php", "path/1.php", "path/5.php", "path/7.php", "path/0.php");
//Put each path AND basename into an array called $filenames
foreach($files as $file) $filenames[basename($file)] = $file;
//Sort $filenames by basename with arsort()
arsort($filenames);
//Check output (listing the paths)
foreach($filenames as $k => $v) echo $v.PHP_EOL;
//Just the values
$filenames_with_paths = array_values($filenames);
print_r($filenames_with_paths);
这将输出:
path/7.php
path/5.php
path/3.php
path/1.php
path/0.php
Array
(
[0] => path/7.php
[1] => path/5.php
[2] => path/3.php
[3] => path/1.php
[4] => path/0.php
)
像这样使用您的代码:
foreach(array_reverse($files) as $filename){}
$files = glob("system/$var/$var/$var/*/*.php");
rsort($files);
foreach($files as $key){
echo $key;
}
您可以使用 usort 对返回的数组进行排序:
$files = glob("system/$var/$var/$var/*/*.php"
usort($files, function ($b, $a) {
return strcmp( substr($a, strrpos($a, '/') + 1),
substr($b, strrpos($b, '/') + 1) );
});
请注意,这将使用字符串比较,因此 10.php 将在 1.php 和 2.php 之间结束。如果那不是您想要的,则必须使用数字比较。即使用 PHP 7 的 "spaceship" 运算符:
$files = glob("system/$var/$var/$var/*/*.php"
usort($files, function ($b, $a) {
return ((int)substr($a, strrpos($a, '/') + 1))
<=> ((int)substr($b, strrpos($b, '/') + 1));
});
编辑:我在回调签名中交换了 $a 和 $b 以反转排序顺序
我有这个文件路径/0.php路径/3.php路径/2.php路径/7.php
我想用 foreach 显示文件,我使用了这个代码:
$files = glob("system/$var/$var/$var/*/*.php");
foreach($files as $file) {
//code here per $file.
//for example:
$basename = basename($file);
echo str_replace(".php",null,$basename) . "<br>";
include $file; //heres i need the path of the files.
}
但我想按 .php 扩展名之前的最大数字对这些文件进行排序,如下所示:
Array
(
[0] => path/7.php
[1] => path/3.php
[2] => path/2.php
[3] => path/0.php
)
所以?
更新,rsort 不适合我,因为它是 path/to/file,在数字之前,我想使用文件的路径。
$files = glob("system/$var/$var/$var/*/*.php");
//Put each file name into an array called $filenames
foreach($files as $i => $file) $filenames[$i] = basename($file);
//Sort $filenames
rsort($filenames);
//Check output
print_r($filenames);
这将输出:
Array
(
[0] => 7.php
[1] => 3.php
[2] => 2.php
[3] => 0.php
)
要保留完整路径,您可以使用路径作为键,使用基本名称作为值:
#For testing:
#$files = array("path/3.php", "path/1.php", "path/5.php", "path/7.php", "path/0.php");
//Put each path AND basename into an array called $filenames
foreach($files as $file) $filenames[basename($file)] = $file;
//Sort $filenames by basename with arsort()
arsort($filenames);
//Check output (listing the paths)
foreach($filenames as $k => $v) echo $v.PHP_EOL;
//Just the values
$filenames_with_paths = array_values($filenames);
print_r($filenames_with_paths);
这将输出:
path/7.php
path/5.php
path/3.php
path/1.php
path/0.php
Array
(
[0] => path/7.php
[1] => path/5.php
[2] => path/3.php
[3] => path/1.php
[4] => path/0.php
)
像这样使用您的代码:
foreach(array_reverse($files) as $filename){}
$files = glob("system/$var/$var/$var/*/*.php");
rsort($files);
foreach($files as $key){
echo $key;
}
您可以使用 usort 对返回的数组进行排序:
$files = glob("system/$var/$var/$var/*/*.php"
usort($files, function ($b, $a) {
return strcmp( substr($a, strrpos($a, '/') + 1),
substr($b, strrpos($b, '/') + 1) );
});
请注意,这将使用字符串比较,因此 10.php 将在 1.php 和 2.php 之间结束。如果那不是您想要的,则必须使用数字比较。即使用 PHP 7 的 "spaceship" 运算符:
$files = glob("system/$var/$var/$var/*/*.php"
usort($files, function ($b, $a) {
return ((int)substr($a, strrpos($a, '/') + 1))
<=> ((int)substr($b, strrpos($b, '/') + 1));
});
编辑:我在回调签名中交换了 $a 和 $b 以反转排序顺序