0

I'm trying to remove duplicates lines from text file. My codes thus work though but its brings error which i have looked through the codes. Everything works. I used the inbuilt function like the array_unique but the array numbers start from 0 instead of 1. So i'm wondering if anyone could help me . Thanks

<?php
hott.txt(content)
vnea@yahoo.com
vnea@yahoo.com
dabrd@gmail.com
vnea@yahoo.com
dabrd@gmail.com
bread@gmail.com
bread@gmail.com
vnea@yahoo.com
dabrd@gmail.com
bread@gmail.com
syeas@hotmail.com
vnea@yahoo.com
vnea@yahoo.com

$contents=file("hott.txt");
$array = [];
foreach ($contents as $k=>$v){

$v = trim($v);
$array[$v]++;
}
print_r($array);


?>

```

2 Answers2

0

with your file output would be as follows

<?php
print_r(file("test.txt"));
?>

Array ( [0] =>vnea@yahoo.com [1] => vnea@yahoo.com [2] => dabrd@gmail.com ...... )

Change needed

    $contents=file("hott.txt");

    $array = array_map('trim', $contents);

    $array = array_unique($array); //add this line to your code
    
    print_r($array);
K.B
  • 885
  • 5
  • 10
0
$lines = explode("\n", $lines);
$lines = array_unique($lines);
$lines = implode("\n", $lines);

Then write the $lines content into your file.

WebPajooh
  • 442
  • 1
  • 3
  • 12