Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
Have you looked at array_unique()? –  PhpMyCoder May 5 '13 at 2:46
    
If this array originates from a query, I'd suggest fixing this in your SQL to reduce the traffic between sql and php. –  Fabrício Matté May 5 '13 at 2:48

3 Answers 3

up vote 2 down vote accepted

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);
?>
share|improve this answer

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));
share|improve this answer
/* here is your array */
$array = 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'
    )
);

/* 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>";
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.