循环复制记录

时间:2016-11-15 21:04:36

标签: php mysql

我目前有一个循环,我试图根据配置文件pic是否不在目录/ pics中来回显电子邮件地址。如何删除重复项,以便只列出一次电子邮件?

<?php 

while ($row_Recordset9 = mysql_fetch_assoc($Recordset9)) {
    $ID = $row_Recordset9['ID'];
    $image = '../pics/' . $ID . '.jpg';  
    if (!file_exists($image)) {
        echo $row_Recordset9['Email'].', ';         
    }
}
?>

2 个答案:

答案 0 :(得分:2)

将电子邮件放入数组中,然后使用array_unique()删除重复项。

$emails = array();
while ($row_Recordset9 = mysql_fetch_assoc($Recordset9)) {
    $ID = $row_Recordset9['ID'];
    $image = '../pics/' . $ID . '.jpg';  
    if (!file_exists($image)) {
        $emails[] = $row_Recordset9['Email'];       
    }
}
$addresses = implode(',', array_unique($emails));
echo $addresses;

答案 1 :(得分:1)

您可以将结果存储到临时数组中并检查:

$temp = array();
while ($row_Recordset9 = mysql_fetch_assoc($Recordset9)) {
    $ID = $row_Recordset9['ID'];
    $image = '../pics/' . $ID . '.jpg';
    $email = $row_Recordset9['Email'];
    if (!file_exists($image) && !in_array($email, $temp)) {
        $temp[] = $email;
        echo $email.', ';
    }
}