2

I have an array with four columns (divided by ";") per index number. The data is coming from a csv file.

Example Data:

John = Firstname
Doe = Lastname
Playground = Description 
[email protected] = Email

print_r($dataArray);

Array
(
 [0] => John;Doe;Playground;[email protected]
 [1] => John;Doe;Playground test;[email protected]
 [2] => John;Doe;test Playground;[email protected]
 [3] => Johnny;Dawson;Test Area;[email protected]
)

Now I want to remove the duplicates with array_unique.

But I only want to compare the "firstname" and the "lastname".

If the firstname and the lastname has multiple results then remove the duplicate entry.

In this case [1] and [2]

$finalArray = array_unique($dataArray); 

array unique will only work if all rows have the same data e.g.

 [0] => John;Doe;Playground;[email protected]
 [1] => John;Doe;Playground;[email protected]

Goal: Final result

Array
(
 [0] => John;Doe;Playground;[email protected]
 [1] => Johnny;Dawson;Test Area;[email protected]
)

What is a good way to handle this case?

6
  • If you have two rows with different data but the same first name, which row do you want to keep? The first or second? Commented Mar 28, 2017 at 17:55
  • this is not important. the first would be okay because the description may vary (which is okay). Commented Mar 28, 2017 at 18:11
  • If it's truly not important, would it be acceptable to filter out only the first and last names and use array_unique on them? Commented Mar 28, 2017 at 18:12
  • if i do that how can i get the rest of the data? description and email? Commented Mar 28, 2017 at 18:13
  • I guess that's what I'm confused about. If description and email are important, how does it not matter which of the duplicate records you keep? Commented Mar 28, 2017 at 18:15

1 Answer 1

2

$a will be unique. notice the array keys that remained the same.

foreach($a as $k=>$v)
{
   list($name,$family) = explode(';', $v);
   if( isset($temp[$name.$family]) )
        unset($a[$k]);
   else
        $temp[$name.$fam] = true;
}
Sign up to request clarification or add additional context in comments.

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.