I try to combine result from several queries and put them into an array. I found there are some duplications exist. What I want to do is keep the first record and remove the following same records if duplication exist. I want to write a function to filter array and remove duplications. Can anyone help me with it? thanks
array(4) {
[0]=>
array(14) {
["user_id"]=>
string(7) "2620613"
["first_name"]=>
string(5) "Susan"
["last_name"]=>
string(9) "Alexander"
["client"]=>
string(7) "Pro"
["ssn"]=>
string(9) "4234324"
["hiredate"]=>
string(10) "2008-04-11"
["hra_id"]=>
string(6) "43244"
["hra_date"]=>
string(10) "2011-08-17"
["hra_maileddate"]=>
NULL
["screening_id"]=>
string(6) "3764551"
["screening_date"]=>
string(10) "2011-08-12"
["screening_maileddate"]=>
NULL
["ref"]=>
string(1) "D"
["mailable"]=>
string(3) "Yes"
}
[1]=>
array(14) {
["user_id"]=>
string(7) "263453"
["first_name"]=>
string(6) "Dharti"
["last_name"]=>
string(6) "Surani"
["client"]=>
string(7) "Pro"
["ssn"]=>
string(9) "345325"
["hiredate"]=>
string(10) "2002-04-29"
["hra_id"]=>
string(6) "3455345"
["hra_date"]=>
string(10) "2010-03-22"
["hra_maileddate"]=>
NULL
["screening_id"]=>
string(6) "234234"
["screening_date"]=>
string(10) "2011-07-11"
["screening_maileddate"]=>
NULL
["ref"]=>
string(1) "D"
["mailable"]=>
string(3) "Yes"
}
[2]=>
array(14) {
["user_id"]=>
string(7) "2685056"
["first_name"]=>
string(4) "Alex"
["last_name"]=>
string(2) "Hu"
["client"]=>
string(7) "Pro"
["ssn"]=>
string(9) "000005562"
["hiredate"]=>
string(10) "2011-09-15"
["hra_id"]=>
string(6) "528948"
["hra_date"]=>
string(10) "2011-07-06"
["hra_maileddate"]=>
NULL
["screening_id"]=>
string(6) "377382"
["screening_date"]=>
string(10) "2011-06-15"
["screening_maileddate"]=>
NULL
["ref"]=>
string(1) "D"
["mailable"]=>
string(3) "Yes"
}
[3]=>
array(14) {
["user_id"]=>
string(7) "2685056"
["first_name"]=>
string(4) "Alex"
["last_name"]=>
string(2) "Hu"
["client"]=>
string(7) "Pro"
["ssn"]=>
string(9) "000005562"
["hiredate"]=>
string(10) "2011-09-15"
["hra_id"]=>
string(6) "528948"
["hra_date"]=>
string(10) "2011-07-06"
["hra_maileddate"]=>
NULL
["screening_id"]=>
string(6) "377971"
["screening_date"]=>
string(10) "2011-09-13"
["screening_maileddate"]=>
NULL
["ref"]=>
string(1) "E"
["mailable"]=>
string(3) "Yes"
}
}
I try to use the following function to remove duplications, but it doesn’t work very well and may cause error on some cases.
$resultDE = array_merge($resultD1, $resultE1, $resultE2);
function specified_array_unique($array)
{
for($i=0; $i<count($array); $i++){
if(($array[$i]['user_id'] == $array[$i+1]['user_id']) && isset($array[$i+1]))
unset($array[$i+1]);
}
return $array;
}
var_dump(super_unique($resultDE));
You need to decide which array key you are going to use to determine whether the record is a duplicate or not. In the following example, you would pass this key into the second parameter.
EDIT I just made this MUCH more efficient..