2014-09-24 104 views
0

我创建了一个包含3个复选框的表单。每个复选框都与数据库中的邮件列表对应。我检查2个复选框时可能会得到重复值。我已经尝试了PHP函数array_unique()和jQuery.unique()函数来从数组中删除所有重复的电子邮件地址。在php数组列表中删除重复的值

的jQuery:

<script> 
$("#sendMessage").submit(function(e) { 
    $("input[type=checkbox]").each(function() { 
     if($(this).is(":checked")) { 
      var string = $(this).val(); 
      $.ajax({ 
       url: "include/checkbox.inc.php", 
       type: "POST", 
       data: ({query: string, type: "nonurgent"}), 
       dataType:"JSON", 
       success: function(data) { 
        keep_cb = data; 
        mail(keep_cb); 
       } 
      }); 
     }    
    }); 
}); 

包括/ checkbox.inc.php:

<?php 

// this page checks which mailing group is selected, urgent or non urgent 

include("mysession_start.inc.php"); 
include("db.inc.php"); 

$testarray = array(); 
$noDupes = array(); 

if(isset($_POST['query'])) { 
$checkbox_value = $_POST['query']; 
} 
if(isset($_POST['type'])) { 
$type = $_POST['type']; 
} 

if($type == "nonurgent") { 
$cb_query = "SELECT email_nonurgent FROM client_addresses WHERE $checkbox_value=1"; 

if($resultq = mysqli_query($link, $cb_query)) { 
    $s_count = mysqli_num_rows($resultq); 

    while($rowq = $resultq->fetch_array()) { 
     $testarray[] = $rowq["email_nonurgent"]; 
     $noDupes = array_unique($testarray); 
    } 

    print json_encode($noDupes); 
} 
} else { 
$cb_query = "SELECT email_urgent FROM client_addresses WHERE $checkbox_value=1";  

if($resultq = mysqli_query($link, $cb_query)) { 
    $s_count = mysqli_num_rows($resultq); 

    while($rowq = $resultq->fetch_array()) { 
     $testarray[] = $rowq["email_urgent"]; 
    } 

    print json_encode($testarray); 
} 
} 

?> 

随着2个复选框点击,它可能是我得到的是相同的阵列中重复的电子邮件地址( $ testarray在PHP页面中)。我在网上搜索了所有内容,但找不到我做错了什么。

+0

您可以包含的代码,你试图用'array_unique()'?不要忘记它会返回一个新数组,而不是像sort()那样修改传入的数组。 – 2014-09-24 15:05:17

+1

“...从数组中复制电子邮件地址”上面的代码中的哪个特定变量是你想要的? – Steve 2014-09-24 15:05:31

+0

这是很多代码,但它看起来像你的问题可以表达[用几句话](http://stackoverflow.com/questions/16063590)。另外,你应该展示你已经尝试过的东西,因为我没有猜测出什么东西可能会出错,就像'array_unique()'](http://stackoverflow.com/q/307650/1446005)一样简单。 – RandomSeed 2014-09-24 15:13:32

回答

0

我不知道为什么array_unique是不是为你工作,但你可以试试老式的方式,而不是将它们添加到摆在首位的阵列,喜欢的东西:

if(!in_array($theEmailAddress, $testarray)){ 
    // add to array 
} 
else{ 
    // more than likely a duplicate 
} 

出于好奇,当你使用它时,array_unique会做什么?

0

能像这样...

$noDupes = array_map("unserialize", array_unique(array_map("serialize", $testarray))); 
+0

尝试过,但服务器仍然向同一收件人发送2封电子邮件 – Beeelze 2014-09-25 07:30:42