[英]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.