PHP删除数组中的重复项

标签 php arrays array-unique

我的 PHP 数组有点问题。首先,他们在这个数组中有数组,我正在尝试删除重复项。我对我的数组做了一个 print_r,它打印出了这个....

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 )
) 

我正在尝试删除 ['comment'] 中的重复项

444 333 111 111 555 第666章

我一直在使用 unique_array,但似乎没有用。这是我试过的....

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

还有

$array = array_unique($array);

foreach($array as $row){

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

我做错了什么? array_unique 不是我的问题的答案吗?

提前致谢

J

最佳答案

array_unique 不是您问题的答案。相反,考虑这样的事情:

$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);

关于PHP删除数组中的重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9244064/

相关文章:

php - 不允许匹配 "[xX][mM][lL]"的处理指令目标

c - 将 C 数组拆分为 n 个相等的部分

java - Array<Vector2> 如何定义它?

php/mysql如何设置特定时间的功能

php - 如何使用 jQuery 将数据发布到 Laravel Controller 方法中

python - 高效的单行代码对列表中每个 `i` 个元素中的第 `n` 个元素进行子采样

php - array_unique PHP 的 Action 数

php - 如何获取数组中某个键的名称

php - 从数组中删除重复项

PHP 取消链接在 codeigniter 中不起作用