Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing duplicate array, from an array of arrays

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

Array
(
[0] => Array
    (
        [id] => 1331
        [shortname] => MCS-115-113C
        [userid] => 663
        [email] => [email protected]
        [username] => FOOBARBAZ
        [nombrecompleto] => asdfasdf
        [lastlogin] => 1367501486
    )
[1] => Array
    (
        [id] => 1331
        [shortname] => MAFA-EOOF
        [userid] => 323
        [email] => [email protected]
        [username] => FOOBARBAZ
        [nombrecompleto] => asdfasdf
        [lastlogin] => 136732186
    )
[2] => Array
    (
        [id] => 1331
        [shortname] => MKT-FOOBAR
        [userid] => 434
        [email] => [email protected]
        [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] => [email protected]
        [username] => FOOBARBAZ
        [nombrecompleto] => asdfasdf
        [lastlogin] => 136732186
    )
[1] => Array
    (
        [id] => 1331
        [shortname] => MKT-FOOBAR
        [userid] => 434
        [email] => [email protected]
        [username] => adsfasdf
        [nombrecompleto] => asdfasdf
        [lastlogin] => 1367234486
    )

How can I accomplish this in PHP?

like image 634
sergserg Avatar asked May 05 '13 02:05

sergserg


People also ask

Which function is used to remove duplicate elements from an array?

The array_unique() function removes duplicate values from an array.


3 Answers

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));
like image 180
Bailey Parker Avatar answered Oct 06 '22 00:10

Bailey Parker


Try this:

<?php

$test=array
(
0 => array
    (
        'id' => '1331',
        'shortname' => 'MCS-115-113C',
        'userid' => '663',
        'email' => '[email protected]',
        'username' => 'FOOBARBAZ',
        'nombrecompleto' => 'asdfasdf',
        'lastlogin' => '1367501486',
    ),
1 => array
    (
        'id' => '1331',
        'shortname' => 'MAFA-EOOF',
        'userid' => '323',
        'email' => '[email protected]',
        'username' => 'FOOBARBAZ',
        'nombrecompleto' => 'asdfasdf',
        'lastlogin' => '136732186'
    ),
2 => array
    (
        'id' => '1331',
        'shortname' => 'MKT-FOOBAR',
        'userid' => '434',
        'email' => '[email protected]',
        '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);
?>
like image 28
Dave Chen Avatar answered Oct 06 '22 00:10

Dave Chen


You can use the below code:

$result = array_unique($data,3);
like image 32
Alireza Salehi Avatar answered Oct 05 '22 23:10

Alireza Salehi