我創建了一個包含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