Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
526 views
Welcome To Ask or Share your Answers For Others

1 Answer

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 )

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...