2016-11-28 67 views
-2

我希望我的程序忽略重複, 我已經使用array_unique但我仍然看到重複 我不知道我在做什麼錯。 因此,我從文本區域獲得電話號碼,然後將它們發送到我的php 任何幫助將不勝感激 這裏是我試過的。我怎樣才能忽略數組中的重複

<script type="text/javascript"> 
    // click and drop code 
     $(document).ready(function(){ 
    $("ul li").click(function(event) { 
    var eid = $(this).attr('id'); 
    $(".text").val($(".text").val() +"\n" + eid); 

}); 
     }); 
//parents_idcelldrag 
    </script> 


<form action="index.php" method="post"> 
<textarea class="text" name = "cellnumbers" readonly></textarea> 
</form> 

    <?php 
// I get this 
$cellnumbers=(isset($_POST['cellnumbers']))? trim($_POST['cellnumbers']): ''; 

    $ids = explode("\n", $cellnumbers); 
    $cleaned = array_unique($ids); 
    foreach($cleaned as $key){ 
    $final_cell .= $key.','; 
    } 

    $final_cell= substr($final_cell,0,-1); 
    echo $final_cell; 
    ?> 
+12

可以提供'$ cellnumbers'一些示例數據嗎? –

+0

如果您沒有向我們展示變量'$ cellnumbers'包含的內容,則此問題無法解決。這段代碼應該使用普通變量'$ cellnumbers'。 – Loko

+2

漢弗萊,你的編輯沒有多大幫助。 '$ _POST ['cellnumbers']'的內容取決於輸入到表單中的數據,這是沒有給出的。什麼'$ _POST ['cellnumbers']'_contain_? – Chris

回答

1

如果$ids有尾隨空格,則可能是這種情況。試着調節值做array_unique前:

$ids = explode("\n", $cellnumbers); 
$ids = array_map('trim', $ids); 
$cleaned = array_unique($ids); 
+4

這是完整的猜測。 – Chris

+0

你是怎麼想出來的,先生,我爲你感到驕傲。我們真的需要這個世界上像你這樣的人。有些人不回答他們只是投票問題,但你完全不同,你說得對。 – humphrey

+1

@humphrey這只是練習,先生。我經常看到類似的問題。我很高興我的回答很有用。 – krlv

1

一個例子將真正幫助這裏雖然做你想要什麼樣的另一種方式:

<?php 

$ids = explode("\n", $cellnumbers); 

// create an array with the values as the keys and their frequencies as the value 
$values_count = array_count_values($ids); 
$cleaned = array_keys($values_count); 

// glue together the values 
$final_cell = implode(',', $cleaned); 

// echo the cleaned result 
echo $final_cell; 
?> 
相關問題