2016-11-28 71 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; 
?> 
相关问题