[英]array_unique not working with filename strings imported from text file
THE PROCESS:过程:
THE PROBLEM:问题:
If my text file already contains the following data: (numbers representing text file lines)如果我的文本文件已经包含以下数据:(数字代表文本文件行)
I then try to share more files including those that are already shared.然后我尝试共享更多文件,包括那些已经共享的文件。 My new text file looks like this: (numbers representing text file lines)
我的新文本文件如下所示:(数字代表文本文件行)
The values above are the exact values I'm dealing with.上面的值是我正在处理的确切值。 I've tried to be as thorough as possible with this explanation which could make things confusing.
我试图尽可能彻底地解释这个可能会使事情变得混乱的解释。 If anyone can provide me with any suggestions they would be greatly appreciated.
如果有人可以向我提供任何建议,他们将不胜感激。 Thanks in advance.
提前致谢。 This is what I've got for code so far:
到目前为止,这是我得到的代码:
THE CODE:代码:
$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);
}
}
To me it looks like the problem is "\n" character.对我来说,问题似乎出在“\n”字符上。 Each line has a new line on the end and when you compare one line that has the newline character and the same line that doesn't they aren't the same.
每行的末尾都有一个新行,当您比较具有换行符的一行和没有换行符的同一行时,它们是不一样的。 I would confirm this by echoing each line from the fgets.
我会通过回显 fgets 中的每一行来确认这一点。 If they are line broken, then you know you are getting the new line character.
如果它们换行了,那么你就知道你得到了新的换行符。
EDIT:编辑:
I would try putting我会尝试把
trim(fgets($fh))修剪(fgets($fh))
by default it should strip the newline character默认情况下它应该去掉换行符
trim specs修剪规格
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.