2

アイテムのリストのtxtファイルを考えてみましょう

qqqqqq

ええええええ

くそったれ

うーん

くそったれ

うーん

999999

また、リスト内の項目の一部が重複しています。php を使用して、複製されたものをすべて削除したテキスト ファイルを出力するにはどうすればよいですか。

結果:

qqqqqq

ええええええ

999999

4

2 に答える 2

4

使用できますarray_unique

そして、コンテンツを右に戻します

$file = fopen("filename.txt", "r");
$members = array();

while (!feof($file)) {
   $members[] = fgets($file);
}

fclose($file);
$unique_members = array(); 
$unique_members = array_unique($members);
var_dump($unique_members);
//write the content back to the file

上記の解決策は、重複のみを削除して一意にするためのものでした。それを指摘してくれたnhahtdhに感謝します。

$count_members = array_count_values($members); 
foreach($count_members as $key=>$value)
{
   if($value == 1) 
       //write it to the file 
}

したがって、array_unique は必要ありません。

于 2013-01-30T22:33:55.267 に答える
1
<?php

$file = file_get_contents('file.txt'); //get file to string
$row_array = explode("\n",$file); //cut string to rows by new line
$row_array = array_count_values(array_filter($row_array));

foreach ($row_array as $key=>$counts) {
    if ($counts==1)
        $no_duplicates[] = $key; 
}

//do what You want
echo '<pre>';
print_r($no_duplicates);

file_put_contents('no_duplicates.txt',$no_duplicates); //write to file. If file don't exist. Create it.
于 2013-01-30T22:40:49.580 に答える