[英]How to make sure that no word is repeated in a csv file using php?
我有一个像这样的.csv文件
file.csv
绿色,黄色,蓝色,白色
这些值将在运行时动态变化,但在file.csv的第一行中保持相同的格式。
在运行时有时会像这样
绿色,蓝色,紫色,紫色,蓝色
这意味着其中一些值是重复的,并且在文件中不应该发生。
无论如何,这将防止使用php重复单词。
下面给出了我到目前为止所做的事情,这似乎是错误的
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);
我知道这没有任何逻辑,但这是我作为先驱者可以做的。
$array=array();
foreach($color as $col1) {
$array[] = $col1;
}
file_put_contents('file.csv',array_unique($array));
这应该是
$file = 'file.csv';
file_put_contents($file, join(',', array_unique(explode(',', file_get_contents($file)))));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.