我尝试将来自多个查询的结果组合在一起并将它们放入数组中。我发现存在一些重复。我想要做的是保留第一条记录,如果存在重复,则删除以下相同的记录。我想写一个函数来过滤数组并删除重复。任何人都可以帮助我吗?感谢
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"
}
}
我尝试使用以下函数来删除重复,但它不能很好地工作,并且可能会在某些情况下导致错误。
$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));
答案 0 :(得分:1)
您需要决定使用哪个数组键来确定记录是否重复。在以下示例中,您将此键传递给第二个参数。
编辑我只是提高了效率......
function array_remove_duplicates ($array, $key) {
$temp = array();
foreach ($array as $k => $record) {
if (isset($record[$key])) {
// We're only going to filter records that actually have the supplied key present
if (in_array($record[$key],$temp)) {
// Key already exists, ignore this one
unset($array[$k]);
} else {
// Key doesn't exist, add it to the tracking array
$temp[] = $record[$key];
}
}
}
// we have filtered out the records we don't want so re-order the indexes and return the array
return array_merge($array);
}
// ...and you could use it like this
$filteredArray = array_remove_duplicates($originalArray,'user_id');
答案 1 :(得分:0)
尝试array_unique($myarray)
(请参阅here)。
编辑:
没关系,这不适用于多维数组。但是,如果您首先将所有数组转换为字符串,然后将它们转换回数组,它就可以工作。
答案 2 :(得分:0)
只需遍历您的数组,检查用户ID:
$userIds = array();
foreach ($users as $key => $user) {
if (in_array($user['user_id'], $userIds)) {
unset($users[$key]);
} else {
$userIds[] = $user['user_id'];
}
}