2012-02-11 127 views
6

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

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

我试图消除在重复的[“评论”]这是

我一直在与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不是我问题的答案吗?

由于提前,

Ĵ

+0

哪些副本要保留? – bozdoz 2012-02-11 21:10:13

回答

4

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); 
+0

真棒,它的工作!谢谢Kolink – user979331 2012-02-11 21:12:08