0

I have a text file in the form of Name@Phone\n. I put the data into an array with explode(). How can I remove duplicate lines (values) based on Phone only? I want to avoid

Eddie@999-999-999
Ed@999-999-999

I want to make this unique based on the Phone.

2
  • 1
    do you need to keep the "Ed" and "Eddie" information? Commented Dec 7, 2011 at 20:51
  • No just to keep one of the records is OK. Commented Dec 7, 2011 at 20:59

3 Answers 3

4
<?php
$result = array();
foreach($dataset as $input):
    $parts = explode('@', $input);
    $result[$parts[1]] = $parts[0];
endforeach;
$result = array_flip($result);
Sign up to request clarification or add additional context in comments.

1 Comment

This also worked well for trying to find the newest directory for each day where there are multiple directorys with YYYY-MM-DD-HH-MM-SS as the format. I used $directories[$directory] = substr($directory,0,10); so that the date would end up being the key and the largest numbered time is the last to be created.
2

If you don't care about the names and just want a list of unique numbers you can replace all characters in the listings that aren't numbers or dashes then do an array_unique():

$arr = array(
  'Eddie@999-999-999',
  'Ed@999-999-999'
);

$arr = preg_replace('/[^\d\-]/', '', $arr);
$arr = array_unique($arr);

print_r($arr);

If you know the numbers are always in that format (name@number) then the response from @Kenaniah works well.

Comments

1

Use:

$result = array();
foreach($arrayData as $data){
    $uniqueKey = strstr($data, '@');//@999-999-999
    $result[$uniqueKey] = $data;
}

var_dump( $result );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.