1 回答

TA貢獻1811條經驗 獲得超4個贊
array_diff將完成這項工作
抓取文件夾中的所有文件:messages
// read all files in messages folder
$dir = 'messages/';
if ($dh = opendir($dir)) {
while(($file = readdir($dh))!== false){
if ($file != "." && $file != "..") { // This line strips out . & ..
$newslist[] = $dir.$file;
}
}
}
closedir($dh);
現在過濾哪個類別中的文件越位(你已經這樣做了)
// Strip file(s) with category Offside
$strip_cat = strtolower('Offside');
$offside_array = array();
$files = glob("messages/*.txt"); // Specify the file directory by extension (.txt)
foreach($files as $file) { // Loop through the files in the directory
$handle = @fopen($file, "r");
if ($handle) {
$lines = file($file); //file into an array
$buffer = $lines[1]; // grab category line
if(strpos(strtolower($buffer), $strip_cat) !== FALSE) { // strtolower; search word not case sensitive
$offside_array[] = $file; // The filename of the match(es)
}
fclose($handle);
}
}
現在比較 2 個數組:
// compare the arrays and strip out the files which contain cat Offside
$filtered_newslist = array_diff($newslist, $offside_array);
$filtered_newslist 是您的新數組,其中包含除具有類別的文件之外的所有文件Offside
您的前循環:
foreach($filtered_newslist as $file) {
$lines = file($file, FILE_IGNORE_NEW_LINES); // filedata into an array
$file_id = $lines[0]; // file id
// and so on...
}
- 1 回答
- 0 關注
- 93 瀏覽
添加回答
舉報