如何将文件分配给作为文件一部分的元素数组

how to assign a file to an array of elements which are part of the file

我有一个包含 .txt 个文件的目录。 在每个 .txt 文件中,一行一行。第二行是 category 行。 txt 文件如下所示:

id-12345678 // id line
sport // category line

几个 类别名称 应该从其他类别中排除,例如 Offside2019 我已经过滤了如下数组:

$blogfiles = glob("data/articles/*.txt"); // array with ALL blogfiles
$all_categories = array();

foreach($blogfiles as $blogfile) { // Loop through the blogfiles in the directory   
    $lines = file($blogfile, FILE_IGNORE_NEW_LINES); // all lines of the txt file into an array
    $all_categories[] = $lines[1]; // category line (2nd line in txt file)
    $excl_categories = array('Offside','2019'); // contains elements that should be excluded
    $filtered_categories = array_diff($all_categories,$excl_categories); //new filtered array of categories

我需要的是一个 blogfiles 的数组,其中的类别已被过滤。我不知道如何将相应的博客文件绑定到过滤后的数组

使用 in_array() 测试每个类别,而不是对整个数组使用 array_diff()

$excl_categories = array('Offside','2019'); // contains elements that should be excluded
$filtered_files = [];
$filtered_categories = [];
foreach($blogfiles as $blogfile) { // Loop through the blogfiles in the directory   
    $lines = file($blogfile, FILE_IGNORE_NEW_LINES); // all lines of the txt file into an array
    $category = $lines[1]; // category line (2nd line in txt file)
    if (!in_array($category, $excl_categories)) {
        $filtered_categories[] = $category;
        $filtered_files[] = $blogfile;
    }
}