dongsuoxi1790 2016-09-24 19:02
浏览 46
已采纳

php:array_unique缺少重复

I have been struggling with a minor issue with the array_unique for a couple of days now.

Somehow the output always leaves the last duplicate in the array.

I am getting the text from a text box in an html form

$IDs = trim($_POST['IDs']);
$IDs = explode("
", $IDs);
$IDs = array_filter($IDs, 'trim');
$ID = array_unique($IDs,0);
print_r($ID);

sample input:

012345 0123456 01234567 012345 0123456 01234567 012345 0123456 01234567 sample output:

Array ( [0] => 012345 [1] => 0123456 [2] => 01234567 [3] => 01234567 )

sample input:

012345 0123456 01234567 012345 0123456 01234567 012345 0123456 sample output:

Array ( [0] => 012345 [1] => 0123456 [2] => 01234567 [3] => 0123456 ) 

not sure why the last duplicate keeps getting missed.

Am sure I am missing something but cannot seem to figure it out.

Added the foreach loop hoping to fix it but even with that I keep getting the same result.

  • 写回答

2条回答 默认 最新

  • dousha7904 2016-09-24 19:11
    关注

    You should use array_map instead of array_filter.

    Like:

    $IDs = trim($_POST['IDs']);
    $IDs = explode("
    ", $IDs);
    $IDs = array_map('trim', $IDs);
    $ID = array_unique($IDs,0);
    print_r($ID);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示