数组删除超过X值的重复条目

时间:2013-03-20 00:13:24

标签: php mysql arrays

PHP 我有两个单个数组,从两个表中填充,带有ID。从一个数组我需要能够删除出现超过X数的相同值的所有ID。 我这样做:

volunteerRoleQuery = mysql_query("SELECT member_no, role_code, volunteer_date FROM         evntrole WHERE volunteer_date > '$today' ");
$numberEvntRole = mysql_numrows($volunteerRoleQuery);

while ($row = mysql_fetch_assoc($volunteerRoleQuery)) {
    $member_no = $row["member_no"];
    array_push($volunteersArray, $member_no);
}

fputcsv($output, array('Member number', 'Full name', 'First name', 'Surname', 'Email', 'Mobile', 'Phone'));

$teamMemQuery = mysql_query("SELECT member_number, activity_code, modify_date FROM  teammem WHERE modify_date > '$today' ");
    $numberTeamMem = mysql_numrows($teamMemQuery);

if ($numberTeamMem > 0) {
    while ($row = mysql_fetch_assoc($teamMemQuery)) {
        $member_no = $row["member_number"];
        array_push($teamMemArray, $member_no);        
    }
}

// todo:只保留成员ID超过x个事件,比如3 //例如1,1,2,3,3,3,3,6,7,7,7,7将仅保留3,7 //这确保我们不会要求会员做志愿者,如果只做了几件事。

$result=array_unique($teamMemArray);

array_diff_ORG_NEW($teamMemArray,$volunteersArray, 'VALUES');

$result=array_unique($teamMemArray);
sort($result);
foreach ($result as &$value) {
        $resultNames = mysql_query("SELECT first_name, surname, mobile, phone_home, e_mail  FROM names WHERE member_no='$value'");
        $rowNames = mysql_fetch_array($resultNames);
        $firstName = $rowNames['first_name'];
        $surname = $rowNames['surname'];

            if ($surname > "") {
                fputcsv($output, array($value, $names, $firstName, $surname, $email, $mobile, $homePhone));
        }
}

1 个答案:

答案 0 :(得分:0)

不确定我是否正确理解了您的需求但是,您是否考虑过: 来自PHP的array_count_values

它为您提供另一个数组,其中包含为关键字找到的匹配项数量。

您可以循环浏览并删除结果大于X的任何键?