简体   繁体   English

PHP 数字文件名排序数组

[英]PHP Sort Array of Numeric Filenames

Have code that right now creates and array of a directory that has text files and gets their modified date and then sorts descending then slices and echos each individually with other parameters.拥有现在创建的代码和一个包含文本文件的目录数组,并获取它们的修改日期,然后降序排序,然后切片并与其他参数单独回显每个。

I am changing it to get the files in a directory and sort by filename which are numeric and then I want it sorted descending then sliced then echoed.我正在更改它以获取目录中的文件并按数字文件名排序,然后我希望它降序排序然后切片然后回显。

$files_listed = array();
foreach (glob('dir/*.txt') as $quip) {
    $files_listed[filemtime($quip)] = $quip;
}
krsort($files_listed);
$master_arc = array_slice($files_listed, 0, 5, true);

// array
foreach($master_arc as $step) {

$arc = file($step, FILE_IGNORE_NEW_LINES);

// print
include 'echo.php';

}

I tried scandir and nothing showed up我试过 scandir 并没有出现

$files_listed = array();
$dir = 'dir/';
$files_listed = scandir($dir, 1);
$master_arc = array_slice($files_listed, 0, 5, true);

// array
foreach($master_arc as $step) {

$arc = file($step, FILE_IGNORE_NEW_LINES);

// print
include 'echo.php';

}

I tried just removing filemtime and it also failed so I am lost.我尝试删除 filemtime 并且它也失败了,所以我迷路了。

$files_listed = array();
foreach (glob('dir/*.txt') as $quip) {
    $files_listed = $quip;
}
arsort($files_listed);
$master_arc = array_slice($files_listed, 0, 5, true);

// array
foreach($master_arc as $step) {

$arc = file($step, FILE_IGNORE_NEW_LINES);

// print
include 'echo.php';

}

This works for me.这对我有用。

<?php

$files_listed = array();

foreach (glob('*.txt') as $quip) {
    $files_listed[] = $quip;
}

arsort($files_listed);
$master_arc = array_slice($files_listed, 0, 5, true);

// array
foreach($master_arc as $step) {
print_r($step);

$arc = file($step, FILE_IGNORE_NEW_LINES);
print_r($arc);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM