我创建了一个包含3个复选框的表单。每个复选框都记录了数据库中的邮件列表。当我选中2个复选框时,我可能会得到重复的值。我已经尝试了PHP函数array_unique()和jQuery.unique()函数来从数组中删除所有重复的电子邮件地址。
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个复选框后,我可能会得到同一个数组中的重复电子邮件地址(php页面中的$ testarray)。我在网上搜索过,但找不到我做错了什么。
答案 0 :(得分:0)
我不知道为什么array_unique
不适合你,但你可以用老式的方式尝试它,而不是首先将它们添加到数组中,例如:
if(!in_array($theEmailAddress, $testarray)){
// add to array
}
else{
// more than likely a duplicate
}
出于好奇,array_unique
在您使用它时会做什么?
答案 1 :(得分:0)
可能是这样的......
$noDupes = array_map("unserialize", array_unique(array_map("serialize", $testarray)));