[英]array_unique not working with filename strings imported from text file
过程:
问题:
如果我的文本文件已经包含以下数据:(数字代表文本文件行)
然后我尝试共享更多文件,包括那些已经共享的文件。 我的新文本文件如下所示:(数字代表文本文件行)
上面的值是我正在处理的确切值。 我试图尽可能彻底地解释这个可能会使事情变得混乱的解释。 如果有人可以向我提供任何建议,他们将不胜感激。 提前致谢。 到目前为止,这是我得到的代码:
代码:
$arr = $_POST['checked'];
$cust = $_POST['custname'];
if ($cust != ""){
$myFile = "CONTA.txt";
//If file exists get previous array from file
if (file_exists("customer/" . $cust . "/" . $myFile)) {
$fh = fopen("customer/" . $cust . "/" . $myFile, 'r') or die("");
while (!feof($fh) ) {
$compare[] = fgets($fh);
}
fclose($fh);
//Combine checkbox array with previous array & eliminate duplicates
$combined = array_unique(array_merge($compare,$arr));
}
else
{
//Since no previous file or array existed. Just use current checkbox array.
$combined = $arr;
}
//Input array into file
$fh = fopen("customer/" . $cust . "/" . $myFile, 'w') or die("can't open file");
foreach ($combined as $value) {
fwrite($fh, $value . "\n");
}
echo "<span class='message'>Items shared successfully!</span>";
fclose($fh);
}
}
对我来说,问题似乎出在“\n”字符上。 每行的末尾都有一个新行,当您比较具有换行符的一行和没有换行符的同一行时,它们是不一样的。 我会通过回显 fgets 中的每一行来确认这一点。 如果它们换行了,那么你就知道你得到了新的换行符。
编辑:
我会尝试把
修剪(fgets($fh))
默认情况下它应该去掉换行符
修剪规格
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.