【发布时间】:2020-04-01 04:12:05
【问题描述】:
我有几个.txt 文件,其中的代码行。每个txt文件的名字都有一个唯一的id:
20191205140624.txt
20191206140309.txt
and so on...
所有的txt文件都在messages文件夹中,我通过这段代码阅读了所有的文件:
// read all files in messages folder
$dir = 'messages/';
if ($dh = opendir($dir)) {
while(($file = readdir($dh))!== false){
if ($file != "." && $file != "..") { // This line strips out . & ..
$entrylist[] = $file;
$lines = file($dir.$file);
$secondline = $lines[1]; // grab the second line
$globalArray[] = $secondline; // put all second lines in an array
}
}
sort($globalArray); // sort array
print_r($globalArray); // show me the array
}
所以 var $entrylist 是一个包含所有 txt 文件的数组。
print_r($globalArray) 向我展示了基于姓氏的排序数组
为了输出 txt 文件,我使用了 foreach:
// sort array
sort($entrylist);
foreach($entrylist as $file){
$entryfiles = 'messages/'.$file;
$lines = file($entryfiles, FILE_IGNORE_NEW_LINES);// filedata into an array
$file_id = $lines[0]; // file id
$surname= $lines[1]; // surname
$address= $lines[3];
and so on...
我的问题:
如何根据每个文件的第二行(姓氏)对foreach中的所有txt文件进行排序?
所以我想要实现的是:sort($entrylist) based on second line of each txt file...
【问题讨论】:
标签: php arrays sorting foreach