【发布时间】:2020-05-25 14:28:33
【问题描述】:
对于平面文件博客,我使用 glob 读取所有博客文件(.txt 文件)。
每个.txt 文件都有不同的行。 .txt 文件如下所示:
id_20200514222532 // id line (0 line)
club
uploads/12.jpg
soccer // title line (3rd line)
comment goes here
14 May 2020 22:25
john
soccer,barcelona
194
4 // likes line (9th line, number of likes)
我想要实现的目标:只输出点赞最多的 5 个标题!
这是我目前所拥有的:
$files = glob("data/articles/*.txt"); // read all files in dir articles
$title_lines = array();
$like_lines = array();
foreach($files as $file) { // Loop the files in the directory
$lines = file($file, FILE_IGNORE_NEW_LINES);
$title_lines[] = strtolower($lines[3]); // grab title line
$like_lines[] = $lines[9]; // grab like line
// $title_lines contains all values of the titles
// $like_lines contains all values of likes
// output now only the 5 titles with the most likes
}
所以我的输出应该如下所示:
Soccer (4) // 4 likes
Swim (3) // 3 likes
Baseball (3) // 3 likes
Volleybal (2) // 2 likes
Athletics (1) // 1 like
【问题讨论】: