【发布时间】:2020-06-20 10:20:30
【问题描述】:
对于平面文件博客系统,我使用.txt 文件,其中存储了所有数据。
txt 文件如下所示:
id_123456 // 1st line is id line
sport // 2nd line is category line
...
我需要的是一组具有特定类别名称的 txt 文件。 这是我到目前为止所拥有的:
$filterthis = 'club';
$filematches = [];
$blogfiles = glob($dir.'/*.txt'); // read all the blogfiles (txt files)
foreach($blogfiles as $file) { // loop through all te files
$lines = file($file, FILE_IGNORE_NEW_LINES); // file in to an array
$category = $lines[1]; // the category line
if (in_array($category, $filterthis)) {
$filematches[] = $file; // array with all the files which have category "club"??
}
所以$filematches 应该包含所有包含“俱乐部”类别的文件,但我没有成功
【问题讨论】:
-
您是否曾希望改用数据库?
-
在
if (in_array($category, $filterthis))- 你认为这两个中的哪一个是数组?根据示例内容,第二行是类别(字符串),您正在寻找$filterthis(字符串) - 两者都不会是数组。也许if( $category==$filterthis ){/* add to output */} -
我知道这是错误的,但我怎样才能实现我想要的呢?
-
为什么不将信息保存为 JSON?我用这种格式制作了一个视频游戏,所以我不必使用数据库。查看此链接以获取示例:stackoverflow.com/questions/2467945/…