我创建了一个包含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页面中)。我在网上搜索了所有内容,但找不到我做错了什么。
您可以包含的代码,你试图用'array_unique()'?不要忘记它会返回一个新数组,而不是像sort()那样修改传入的数组。 – 2014-09-24 15:05:17
“...从数组中复制电子邮件地址”上面的代码中的哪个特定变量是你想要的? – Steve 2014-09-24 15:05:31
这是很多代码,但它看起来像你的问题可以表达[用几句话](http://stackoverflow.com/questions/16063590)。另外,你应该展示你已经尝试过的东西,因为我没有猜测出什么东西可能会出错,就像'array_unique()'](http://stackoverflow.com/q/307650/1446005)一样简单。 – RandomSeed 2014-09-24 15:13:32