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)
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

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

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


?>

```
2
  • If possible please add ex. file to question Commented Jul 2, 2022 at 17:27
  • ok sir i will let me see Commented Jul 2, 2022 at 17:34

2 Answers 2

0

with your file output would be as follows

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

Array ( [0] =>[email protected] [1] => [email protected] [2] => [email protected] ...... )

Change needed

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

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

    $array = array_unique($array); //add this line to your code
    
    print_r($array);
Sign up to request clarification or add additional context in comments.

4 Comments

Notice: Undefined index: [email protected] in . It works tho but got that error
Updated answer ..please check
it is worth to you..please do upvote
alright i will, thanks sir
0
$lines = explode("\n", $lines);
$lines = array_unique($lines);
$lines = implode("\n", $lines);

Then write the $lines content into your file.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.