简体   繁体   English

array_unique 不适用于从文本文件导入的文件名字符串

[英]array_unique not working with filename strings imported from text file

THE PROCESS:过程:

  1. User checks checkboxes to share files with customer accounts用户选中复选框以与客户帐户共享文件
  2. Checkbox values are compared against an array stored in a txt file from the customers folder将复选框值与存储在客户文件夹中的 txt 文件中的数组进行比较
  3. The arrays are compared by being merged into one array using array_merge() arrays 通过使用 array_merge() 合并到一个数组中进行比较
  4. The duplicates are eliminated using array_unique()使用 array_unique() 消除重复项
  5. New array written to txt file新数组写入txt文件

THE PROBLEM:问题:

If my text file already contains the following data: (numbers representing text file lines)如果我的文本文件已经包含以下数据:(数字代表文本文件行)

  1. M HTH A277 Frame Off STD Specs 02-01-12.pdf M HTH A277 框架关闭标准规格 02-01-12.pdf
  2. M HTH A277 Frame On STD Specs 02-01-12.pdf M HTH A277 框架标准规格 02-01-12.pdf
  3. M HTH Option Can Price List 02-02-2012.xls M HTH Option Can 价格表 02-02-2012.xls

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)我的新文本文件如下所示:(数字代表文本文件行)

  1. M HTH A277 Frame Off STD Specs 02-01-12.pdf M HTH A277 框架关闭标准规格 02-01-12.pdf
  2. (blank) (空白的)
  3. M HTH A277 Frame On STD Specs 02-01-12.pdf M HTH A277 框架标准规格 02-01-12.pdf
  4. (blank) (空白的)
  5. M HTH Option Can Price List 02-02-2012.xls M HTH Option Can 价格表 02-02-2012.xls
  6. (blank) (空白的)
  7. (blank) (空白的)
  8. M HTH A277 Frame Off STD Specs 02-01-12.pdf M HTH A277 框架关闭标准规格 02-01-12.pdf
  9. M HTH A277 Frame On STD Specs 02-01-12.pdf M HTH A277 框架标准规格 02-01-12.pdf
  10. M HTH Option Can Price List 02-02-2012.xls M HTH Option Can 价格表 02-02-2012.xls
  11. Valley Creek Estates - 2010.pdf谷溪庄园 - 2010.pdf

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM