2

Whats the quickest way to upload a CSV with PHP whilst removing duplicates (the phone number).

Example:

Kyle,Hudson,447000000000,[email protected],CUST-1,CUST-2,CUST-3
John,Doe,447000000001,[email protected],CUST-1,CUST-2,CUST-3
John,Doe,447000000001,[email protected],CUST-1,CUST-2,CUST-3
Jack,Doe,447000000004,[email protected],CUST-1,CUST-2,CUST-3 

Should become:

Kyle,Hudson,447000000000,[email protected],CUST-1,CUST-2,CUST-3
John,Doe,447000000001,[email protected],CUST-1,CUST-2,CUST-3
Jack,Doe,447000000004,[email protected],CUST-1,CUST-2,CUST-3 

I know how to upload the CSV ect, I just need to know how to remove the duplicates.

Would I need to create an array or something similar and then use a function like array_unique?

Your help is appriciated :)

6 Answers 6

2

Yeah, like you said. Upload it, put all entries in an array, throw array_unique over it and continue.

Sign up to request clarification or add additional context in comments.

2 Comments

Gerben, what is the best way to convert an csv to an array?
Well, there is: php.net/manual/en/function.fgetcsv.php But, imho, you're better off making something yourself. So open your file with file_get_contents(), then explode() with "\n", you'll now have an array with every line. Then explode again, this time on the comma, semicolon or whatever seperator you're using. Now you have every item in your new array.
0

array_unique — Removes duplicate values from an array

http://php.net/manual/en/function.array-unique.php

If data is so huge PHP arrays are expensive

Comments

0

Also you can use array_flip for passing values as array key.

Keys of array cannot be duplicate.

Comments

0

You want to read the csv by each line and then you want to use an auxilliary array to store and check for duplicates.

Comments

0

You can use array_unique, but then you need to create a array.. You got a strange long number in your csv?(447000000000) i assume thats unique for every user. So make a key from that number and do it like underneave here.. So i suggest to create a array like this:

Array(
  [447000000000] => Array (
    "name" => "Kyle"
    "lastname" => "Hudson",
    "id" => 447000000000
    [.........]
  ),
  [447000000001] => Array (
    "name" => "John"
    "lastname" => "Doe",
    "id" => 447000000001
    [.........]
  )
  [.........]
)

and now you can use array_unique on that

Comments

0

The code you need to make this work is pretty staight forward!

Here you go:

<?php

$myCSV = file($_FILES['file']['tmp_name']);
//duplicates
var_dump($myCSV);
$myCSV = array_unique($myCSV);
//unique
var_dump($myCSV);

//now handle it how you need

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.