开发者

php csv upload remove duplicates

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

Example:

Kyle,Hudson,447000000000,me@you.com,CUST-1,CUST-2,CUST-3
John,Doe,447000000001,me@you.com,CUST-1,CUST-2,CUST-3
John,Doe,447000000001,me@you.com,CUST-1,CUST-2,CUST-3
Jack,Doe,447000000004,me@you.com,CUST-1,CUST-2,CUST-3 

Should become:

Kyle,Hudson,447000000000,me@you.com,CUST-1,CUST-2,CUST-3
John,Doe,447000000001,me@you.com,CUST-1,CUST-2,CUST-3
Jack,Doe,447000000004,me@you.com,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 ap开发者_JS百科priciated :)


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


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


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

Keys of array cannot be duplicate.


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


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜