7

这是我正在处理的数组的示例:

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
    )

就我而言,我想比较username数组中的元素并删除重复项。

所以在这种情况下,我只会返回两个元素,用户名FOOBARBAZadsfasdf

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
    )

我怎样才能在 PHP 中做到这一点?

4

5 回答 5

11

这很容易通过array_unique(). 但是,默认情况下,此函数将所有内容转换为字符串,因此您需要传递SORT_REGULAR常量。(演示

<?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));
于 2013-05-05T02:55:26.867 回答
8

尝试这个:

<?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);
?>
于 2013-05-05T02:47:27.947 回答
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>";
于 2013-05-05T12:11:40.623 回答
0

使用以下代码:

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

$isContainDupes = count($array) !== count(array_unique($array, SORT_REGULAR));
var_dump($isContainDupes); // true
于 2019-02-18T12:07:49.927 回答
0

您可以使用以下代码:

$result = array_unique($data,3);
于 2019-12-05T15:11:37.797 回答