0

I want to remove duplicates from input array by only value of user_id

Input array:

array (size=3)
  0 => 
    array (size=3)
      'user_id' => string '3' (length=1)
      'rate' => string '0' (length=1)
      'note' => string 'a' (length=1)
  1 => 
    array (size=3)
      'user_id' => string '3' (length=1)
      'rate' => string '2' (length=1)
      'note' => string 'b' (length=1)
  2 => 
    array (size=3)
      'user_id' => string '4' (length=1)
      'rate' => string '3' (length=1)
      'note' => string 'c' (length=1)

Output:

array (size=3)
  0 => 
    array (size=3)
      'user_id' => string '3' (length=1)
      'rate' => string '0' (length=1)
      'note' => string 'a' (length=1)
  1 => 
    array (size=3)
      'user_id' => string '4' (length=1)
      'rate' => string '3' (length=1)
      'note' => string 'c' (length=1)

So far:

$result1 = array_unique($inputArray, SORT_REGULAR);
$result2 = array_map("unserialize", array_unique(array_map("serialize", $inputArray)));
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Ing. Michal Hudak
  • 5,338
  • 11
  • 60
  • 91

4 Answers4

2

This should work for you:

<?php

    $arr = array(
            array(
                'user_id' => '3',
                'rate' => '0',
                'note' => 'a'
            ),
            array(
                'user_id' => '3',
                'rate' => '2',
                'note' => 'b'
            ),
            array(
                'user_id' => '4',
                'rate' => '3',
                'note' => 'c' 
            )
        );

    $tmp = array();

    foreach($arr as $key => $innerArray) {
        if(in_array($innerArray["user_id"], $tmp))
            unset($arr[$key]);
        else
            $tmp[] = $innerArray["user_id"];
    }

    print_r($arr);

?>

EDIT:

If you want a more fancyer version:

$tmp = array();
$result = array();

$result = array_values(array_filter(array_map(function($innerArray)use(&$tmp){
    if(!in_array($innerArray["user_id"], $tmp)) {
        $tmp[] = $innerArray["user_id"];
        return $innerArray;
    }
}, $arr)));

print_r($result);
Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • 1
    The unset isn't required, right? In other languages if you unset a key in an array while looping over that array bad things can happen. Is behaviour guaranteed to be correct in php? – Sumurai8 Feb 23 '15 at 10:22
  • @Sumurai8 Here it doesn't makes any problem – Rizier123 Feb 23 '15 at 10:23
2

If you're running PHP >= 5.6 then the following will work:

$data = [
    [
      'user_id' => '3',
      'rate' => '0',
      'note' => 'a',
    ],
    [
      'user_id' => '3',
      'rate' => '2',
      'note' => 'b',
    ],
    [
      'user_id' => '4',
      'rate' => '3',
      'note' => 'c',
    ],
];

$result = array_filter(
    $data,
    function ($value, $key) use ($data) {
        return $key === array_search($value['user_id'], array_column($data,'user_id'));
    },
    ARRAY_FILTER_USE_BOTH
);
var_dump($result);
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • Lol just saw it :D wow I was going crazy. I had the manual open in German (http://php.net/manual/de/function.array-filter.php) and there wasn't anything about flags. Then I changed it to English (http://php.net/manual/en/function.array-filter.php) – Rizier123 Feb 23 '15 at 10:34
  • It's actually pretty useful for a whole host of situations, and I'm glad I'm running 5.6 on my production boxes – Mark Baker Feb 23 '15 at 10:35
  • Shows how long it takes to update the PHP docs in their different translations though – Mark Baker Feb 23 '15 at 10:36
  • Yep! That's why I normally use it in English. (Also easier to read) But wow I was like 10min in the manual and I didn't noticed it :D – Rizier123 Feb 23 '15 at 10:38
0

If the user_id is just an int or a string, you could use it as the index, like this:

$a = [
    [
        'id' => 1,
    ],
    [
        'id' => 2,
    ],
    [
        'id' => 3,
    ],
    [
        'id' => 1,
    ],
];

$b = array_reduce($a, function($carry, $item) {
    $carry[$item['id']] ??= $item;
    return $carry;
}, []);

var_dump($b);

// Or more plainly like this:
$b = [];
foreach ($a as $val) {
    $b[$val['id']] ??= $val;
}

var_dump($b);

// If you want numerical indexes, just do this
$b = array_values($b);
Lars Nyström
  • 5,786
  • 3
  • 32
  • 33
  • @mickmackusa I can't even remember writing this answer anymore, but I see your point. I've now fixed the answer to keep the first occurring element instead of last occurring element. – Lars Nyström Oct 13 '21 at 08:47
0

1.Simply you can try this

$result = array_reverse(array_values(array_column(
    array_reverse($data),
    null,
    'user_id'
)));

It may also be NULL to return complete arrays or objects (this is useful together with index_key to reindex the array).

2.If you are fetching record using sql query you can used GROUP BY clause like below :

GROUP BY user_id    

Ref :php remove duplicates from multidimensional array by value

Bhargav Variya
  • 725
  • 1
  • 10
  • 18
  • I was just about to tell you that I recommend this advice, but then the link in your answer points to the same technique which I had coincidentally recommended. – mickmackusa Oct 13 '21 at 07:14