array_diff php 中只返回一个不匹配的值,即使它不重复



在代码下面运行时array_diff只返回一个值。但是,它应该返回两个值。我的第一个数组保存:

access.2018.08.09.log
access.2018.08.10.log
access.2018.08.12.log

我的第二个数组保存:

access.2018.08.09.log

array_diff()仅返回:access.2018.08.12.log

有人可以指导为什么会发生这种情况。

<?php
$files = scandir('C:wamp64wwwMyLogslogsfiles');
foreach($files as $file) {
if($file == '.' || $file == '..') continue;
$S1=array($file.PHP_EOL);
print_r($S1);
}
$S2 =explode("n", file_get_contents('uplodedregistry.txt'));
$result = array_diff_assoc($S1, $S2);
print_r($result);       
?>

你不断覆盖你的$S1变量 - 所以在循环结束时,它只会保存一个元素 - 循环中的最后一个值。相反,在循环之前实例化数组,并在循环中追加到它。

<?php
$files = scandir('C:wamp64wwwMyLogslogsfiles');
$S1 = array(); // Init the array here
foreach($files as $file) {
if($file == '.' || $file == '..')
continue;
$S1[] = $file.PHP_EOL; // Append to $S1
}
$S2 = explode("n", file_get_contents('uplodedregistry.txt'));
$result = array_diff($S1, $S2);
print_r($result);

相关内容

最新更新