我有一個包含.txt文件的目錄。在每個.txt文件中都是行,在彼此之下。第二行是類別行。一個 txt 文件如下所示:id-12345678 // id linesport // category line應該從其他類別中排除幾個類別名稱,例如Offside和2019 我已經過濾了如下數組:$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其中的類別已被過濾。我不知道如何將相應的博客文件綁定到過濾后的數組
1 回答

達令說
TA貢獻1821條經驗 獲得超6個贊
用于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;
}
}
- 1 回答
- 0 關注
- 167 瀏覽
添加回答
舉報
0/150
提交
取消