doutongya8378 2012-02-11 21:05
浏览 34
已采纳

PHP从数组中删除重复项

I am having a bit of an issue with my PHP array. First thing they have arrays inside of this array and I am trying to remove duplicates. I did a print_r of my array and it printed out this....

Array (
   [0] => Array ( [creditfeeid] => 318 [client] => Test 1 [adjustment] => 444.00 [issuedate] => 2012-02-10 20:27:00 [isrecurring] => No [comment] => 444 [commission] => 17.76 )
   [1] => Array ( [creditfeeid] => 319 [client] => Test 1 [adjustment] => 333.00 [issuedate] => 2012-02-10 20:27:00 [isrecurring] => No [comment] => 333 [commission] => 9.99 )
   [2] => Array ( [creditfeeid] => 320 [client] => Test 1 [adjustment] => 111.00 [issuedate] => 2012-02-10 20:27:00 [isrecurring] => No [comment] => 111 [commission] => 1.11 )
   [3] => Array ( [creditfeeid] => 321 [client] => Test 1 [adjustment] => 222.00 [issuedate] => 2012-02-10 00:00:00 [isrecurring] => No [comment] => 111 [commission] => 2.22 )
   [4] => Array ( [creditfeeid] => 292 [client] => Test 1 [adjustment] => 555.00 [issuedate] => 2012-01-25 13:04:43 [isrecurring] => Yes [comment] => 555 [commission] => 5.00 )
   [5] => Array ( [creditfeeid] => 317 [client] => Test 2 [adjustment] => 666.00 [issuedate] => 2012-02-10 00:00:00 [isrecurring] => No [comment] => 666 [commission] => 39.96 )
) 

I am trying to remove the duplicates in the ['comment'] which are

444 333 111 111 555 666

I have been working with unique_array and that doesn't seem to do the trick. This is what I have tried....

foreach($array as $row){
if(array_unique($row['comment'])){
    continue;
}else{
    echo $row['comment'] . "<br/>";
}
}

also

$array = array_unique($array);

foreach($array as $row){

        echo $row['comment'] . "<br/>";
}

What am I doing wrong? Is array_unique not the answer to my problem?

Thanks in advance,

J

  • 写回答

1条回答 默认 最新

  • douyiyi5284 2012-02-11 21:08
    关注

    array_unique is not the answer to your problem. Instead, consider something like this:

    $new_array = Array();
    foreach($old_array as $a) {
        if( !isset($new_array[$a['comment']]))
            $new_array[$a['comment']] = $a;
    }
    $new_array = array_values($new_array);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来