1

I am in need to open a text file (file.txt) which contains data in the following format :-

dave : 50lb : hlof
jimmy : 55lb : okay
dave : 12lb : krsho

I want to remove lines that has duplicate start word So the result would look like this :-

dave : 50lb : hlof
jimmy : 55lb : okay

I've been thinking about using array_unique but didn't worked so any idea how it can be done .. maybe can be with regex !

Update

my try was

$lines = file('file.txt');
$lines = array_unique($lines);
file_put_contents('file.txt', implode($lines));

but didn't worked cause it only compare the whole line if not the same will then consider it different

Reham Fahmy
  • 1,058
  • 1
  • 7
  • 10
  • *but didn't worked* Why did it not worked? Show us your attempt! BTW: You have 3 different lines which you show us here – Rizier123 Mar 13 '15 at 20:45
  • @Rizier123 i've updated the question with my try using `array_unique` ... – Reham Fahmy Mar 13 '15 at 20:48
  • And you know that every file line which you show us here is unique?! Are you trying to compare only the first column (name) ? – Rizier123 Mar 13 '15 at 20:49
  • @Rizier123 yes but i wonder if i can consider line that has same start word just before `:` are duplicated and to be removed. so `dave : 50lb : hlof` and `dave : 12lb : krsho` have same start word `dave` . – Reham Fahmy Mar 13 '15 at 20:51
  • Rizier123 yep, i wanna compare based only on first colum – Reham Fahmy Mar 13 '15 at 20:51

2 Answers2

5

This should work for you:

(Here I first go through each element of $lines with array_map() then I explode() every value with : and return the first element. After this I use array_intersect_key() with the created array from before where I use array_unique() to get the unique keys and get the intersect with the full array)

<?php

    $lines = file("test.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

    $lines = array_intersect_key($lines, array_unique(array_map(function($v){
        return trim(explode(":", $v)[0]);
    }, $lines)));

    print_r($lines);

?>

Output:

Array ( [0] => dave : 50lb : hlof [1] => jimmy : 55lb : okay )
Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • @hwnd Thanks :), But I still have the feeling that there most be an easier way, but I can't think about it. – Rizier123 Mar 13 '15 at 21:06
  • 1
    This method was the shortest way. – hwnd Mar 13 '15 at 21:07
  • OMG i thought it was impossible to do so .. you are so genius .. Thanks a lot. – Reham Fahmy Mar 13 '15 at 21:13
  • 1
    @Rizier123 I just want to confirm it works perfect as you've said without any error on PHP 5.3,5.4,5.5,5.6 since they all support `functions.anonymous` that answer did not just fixed my problem but also pushed me to move on and upgrade PHP to most stable version .. thanks a lot. – Reham Fahmy Mar 15 '15 at 09:50
  • @RehamFahmy cool! Glad I could help. If you want to to make it more backwards compatible you could use [`create_function()`](http://php.net/manual/en/function.create-function.php). But I think we live in 2015 and php 5.3 or 5.4 should be standard – Rizier123 Mar 15 '15 at 09:53
0

What errors or problems do you have with array_unique as it works for me:

$lines = file('foobar.txt');
$lines = array_unique($lines);

Update: Added before question was expanded

LinkBerest
  • 1,281
  • 3
  • 22
  • 30