Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Remove Duplicates from an array

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

like image 735
user979331 Avatar asked Feb 11 '12 21:02

user979331


People also ask

How do you remove duplicates from an array?

We can remove duplicate element in an array by 2 ways: using temporary array or using separate index. To remove the duplicate element from array, the array must be in sorted order. If array is not sorted, you can sort it by calling Arrays. sort(arr) method.

What is Array_keys () used for in PHP?

The array_keys() is a built-in function in PHP and is used to return either all the keys of and array or the subset of the keys. Parameters: The function takes three parameters out of which one is mandatory and other two are optional.

How can I get unique values from two arrays in PHP?

You can use the PHP array_unique() function and PHP array_merge() function together to merge two arrays into one array without duplicate values in PHP.


1 Answers

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);
like image 88
Niet the Dark Absol Avatar answered Sep 21 '22 02:09

Niet the Dark Absol