php - 如何使用php从txt文件中读取所有第二行并将它们放入1个变量中
问题描述
我有很多 .txt 文件,其中有几行内容。要获取文件夹消息中的所有 txt 文件,我使用以下代码:
// read all files in messages folder
$dir = 'messages/';
if ($dh = opendir($dir)) {
while(($file = readdir($dh))!== false){
if ($file != "." && $file != "..") { // This line strips out . & ..
$files_list[] = $file;
}
}
}
要从 txt 文件中获取第二行,我知道我可以使用它:
$lines = file($file);
$secondline = $lines[1];
但是我怎样才能从所有 txt 文件中获取所有第二行并将其放入变量中,以便我可以对其进行排序?就像是。rsort($all_secondlines_allfiles);
解决方案
将第二行放入全局数组,然后使用 r/sort() 函数根据需要对其进行排序。
$globalArray = [];
$dir = 'messages/';
if ($dh = opendir($dir)) {
while(($file = readdir($dh))!== false){
if ($file != "." && $file != "..") { // This line strips out . & ..
$lines = file($dir.$file);
$secondline = $lines[1];
$globalArray[] = $secondline;
}
}
sort($globalArray);
print_R($globalArray);
}
推荐阅读
- c++ - C++ 中的 OpenSSL 加密/解密命令行
- django - 如何为 3 个模型映射 ForeignKey 关系 Django
- vba - OutLook VBA 电子邮件或通知导致越界错误
- r - 如何将列一式三份并重新排列
- mongodb - 我可以发送任何其他参数而不是 id,以获取 mongoose 的功能以从 mongodb 获取特定数据
- android - Android Drag and Drop - 使拖动的视图在布局中不可见
- android - 是否可以在没有 SIM 卡的情况下制作使用蜂窝数据的 Android 应用程序?
- android - Flutter svg 加载失败
- ios - iOS 应用保留旧的推送通知键
- c# - 向 MongoDb 中的现有字典添加键,同时使用 C# 替换其他字段