简体   繁体   English

如何使用php确保csv文件中没有单词重复?

[英]How to make sure that no word is repeated in a csv file using php?

I have a .csv file like this 我有一个像这样的.csv文件

file.csv file.csv

green,yellow,blue,white 绿色,黄色,蓝色,白色

these values will change dynamically during runtime but stays in the same format in the single first line in the file.csv. 这些值将在运行时动态变化,但在file.csv的第一行中保持相同的格式。

During runtime sometimes it comes like this 在运行时有时会像这样

green,blue,violet,violet,blue 绿色,蓝色,紫色,紫色,蓝色

that means some of these values are repeating and that should not have happened in the file. 这意味着其中一些值是重复的,并且在文件中不应该发生。

Is there anyway which will prevent the word repetition using php.? 无论如何,这将防止使用php重复单词。

what I have done so far is given below which seems to be wrong 下面给出了我到目前为止所做的事情,这似乎是错误的

First I took the file into an array , $color
$array=array();
foreach($color as $col1)
   {
      foreach($color as $col2)
      {
         if(strcmp($col1,$col2)!=0)
         {
            $array=$col1;
         }
      }
   }
file_put_contents('file.csv',$array);

I know this doesn't have any logic, but this is what i could do as a pioneer . 我知道这没有任何逻辑,但这是我作为先驱者可以做的。

$array=array();
foreach($color as $col1) {
    $array[] = $col1;
}
file_put_contents('file.csv',array_unique($array));

This should be 这应该是

$file = 'file.csv';
file_put_contents($file, join(',', array_unique(explode(',', file_get_contents($file)))));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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