这是我正在反对的数组的一个例子:
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
元素并删除重复项。
所以在这种情况下,我只会返回两个元素,即用户名FOOBARBAZ
和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
)
如何在PHP中完成此操作?
答案 0 :(得分:5)
试试这个:
<?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);
?>
答案 1 :(得分:5)
使用array_unique()
这很容易实现(并且内部很快)。但是,默认情况下,此函数会将所有内容转换为字符串,因此您需要传递SORT_REGULAR
常量。 (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));
答案 2 :(得分: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>";
答案 3 :(得分:0)
使用以下代码:
$array = [
["a"],
["a"],
["b"],
];
$isContainDupes = count($array) !== count(array_unique($array, SORT_REGULAR));
var_dump($isContainDupes); // true
答案 4 :(得分:0)
您可以使用以下代码:
$result = array_unique($data,3);