9

Here's an example of the array I'm working against:

Array
(
[0] => Array
    (
        [id] => 1331
        [shortname] => MCS-115-113C
        [userid] => 663
        [email] => asdfasfd@asdfasfd.br
        [username] => FOOBARBAZ
        [nombrecompleto] => asdfasdf
        [lastlogin] => 1367501486
    )
[1] => Array
    (
        [id] => 1331
        [shortname] => MAFA-EOOF
        [userid] => 323
        [email] => asdfasfd@asdfasfd.br
        [username] => FOOBARBAZ
        [nombrecompleto] => asdfasdf
        [lastlogin] => 136732186
    )
[2] => Array
    (
        [id] => 1331
        [shortname] => MKT-FOOBAR
        [userid] => 434
        [email] => asdfasfd@asdfasfd.br
        [username] => adsfasdf
        [nombrecompleto] => asdfasdf
        [lastlogin] => 1367234486
    )

In my case, I want to compare the username element in the array and delete duplicates.

So in this case, I would only return two elements, username FOOBARBAZ and adsfasdf:

Array
(
[0] => Array
    (
        [id] => 1331
        [shortname] => MAFA-EOOF
        [userid] => 323
        [email] => asdfasfd@asdfasfd.br
        [username] => FOOBARBAZ
        [nombrecompleto] => asdfasdf
        [lastlogin] => 136732186
    )
[1] => Array
    (
        [id] => 1331
        [shortname] => MKT-FOOBAR
        [userid] => 434
        [email] => asdfasfd@asdfasfd.br
        [username] => adsfasdf
        [nombrecompleto] => asdfasdf
        [lastlogin] => 1367234486
    )

How can I accomplish this in PHP?

sergserg
  • 21,716
  • 41
  • 129
  • 182

5 Answers5

13

This is simple to achieve (and quick since its an internal) with array_unique(). However, by default this function casts everything to a string, so you will need to pass the SORT_REGULAR constant. (Demo)

<?php
$data = array(
    array(
        'id' => 0,
        'title' => 'Abc'
    ),
    array(
        'id' => 2,
        'title' => 'Def',
        'content' => 'Stackoverflow!'
    ),
    array(
        'id' => 0,
        'title' => 'Abc'
    )
);

var_dump(array_unique($data, SORT_REGULAR));
Bailey Parker
  • 15,599
  • 5
  • 53
  • 91
9

Try this:

<?php

$test=array
(
0 => array
    (
        'id' => '1331',
        'shortname' => 'MCS-115-113C',
        'userid' => '663',
        'email' => 'asdfasfd@asdfasfd.br',
        'username' => 'FOOBARBAZ',
        'nombrecompleto' => 'asdfasdf',
        'lastlogin' => '1367501486',
    ),
1 => array
    (
        'id' => '1331',
        'shortname' => 'MAFA-EOOF',
        'userid' => '323',
        'email' => 'asdfasfd@asdfasfd.br',
        'username' => 'FOOBARBAZ',
        'nombrecompleto' => 'asdfasdf',
        'lastlogin' => '136732186'
    ),
2 => array
    (
        'id' => '1331',
        'shortname' => 'MKT-FOOBAR',
        'userid' => '434',
        'email' => 'asdfasfd@asdfasfd.br',
        'username' => 'adsfasdf',
        'nombrecompleto' => 'asdfasdf',
        'lastlogin' => '1367234486'
    )
);

$userdupe=array();

foreach ($test as $index=>$t) {
    if (isset($userdupe[$t["username"]])) {
        unset($test[$index]);
        continue;
    }
    $userdupe[$t["username"]]=true;
}

print_r($test);
?>
Dave Chen
  • 10,887
  • 8
  • 39
  • 67
1

You can use the below code:

$result = array_unique($data,3);
Das_Geek
  • 2,775
  • 7
  • 20
  • 26
  • 2
    Welcome to Stack Overflow! Thank you for this code snippet, which might provide some limited short-term help. A proper explanation [would greatly improve](//meta.stackexchange.com/q/114762) its long-term value by showing *why* this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. – Toby Speight Dec 05 '19 at 15:43
0
/* here is your array */
$array = array(
    0 => array(
            'id' => '1331',
            'shortname' => 'MCS-115-113C',
            'userid' => '663',
            'email' => 'asdfasfd@asdfasfd.br',
            'username' => 'FOOBARBAZ',
            'nombrecompleto' => 'asdfasdf',
            'lastlogin' => '1367501486',
    ),
    1 => array(
            'id' => '1331',
            'shortname' => 'MAFA-EOOF',
            'userid' => '323',
            'email' => 'asdfasfd@asdfasfd.br',
            'username' => 'FOOBARBAZ',
            'nombrecompleto' => 'asdfasdf',
            'lastlogin' => '136732186'
    ),
    2 => array(
            'id' => '1331',
            'shortname' => 'MKT-FOOBAR',
            'userid' => '434',
            'email' => 'asdfasfd@asdfasfd.br',
            'username' => 'adsfasdf',
            'nombrecompleto' => 'asdfasdf',
            'lastlogin' => '1367234486'
    )
);

/* initializing an array to store usernames to compare */
$userNames = array();
/* looping through array */
foreach($array as $key=>$value){
    if(!empty($userNames) && in_array($value['username'],$userNames)) unset($array[$key]);  //unset from $array if username already exists
    $userNames[] = $value['username'];  // creating username array to compare with main array values
}

/* print updated array */
echo "<pre>";print_r($array);echo "</pre>";
Ankit Gupta
  • 189
  • 12
0

Use the following code:

$array = [
    ["a"],
    ["a"],
    ["b"],
];

$isContainDupes = count($array) !== count(array_unique($array, SORT_REGULAR));
var_dump($isContainDupes); // true
ziishaned
  • 4,944
  • 3
  • 25
  • 32