亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將文件分配給作為文件一部分的元素數組

如何將文件分配給作為文件一部分的元素數組

PHP
慕尼黑的夜晚無繁華 2023-05-26 16:00:44
我有一個包含.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;

    }

}


查看完整回答
反對 回復 2023-05-26
  • 1 回答
  • 0 關注
  • 167 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號