删除重复阵列,从阵列
问题描述:
的数组下面是我的工作对数组的一个例子:删除重复阵列,从阵列
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
)
在我的情况,我想在数组中的元素username
比较和删除重复。
因此,在这种情况下,我只会返回两个元素,用户名和FOOBARBAZ
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
)
我怎样才能做到这一点在PHP?
答
试试这个:
<?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);
?>
答
这是简单的实现(和快,因为它的内部)与array_unique()
。但是,默认情况下,该函数将所有内容都转换为字符串,因此您需要传递SORT_REGULAR
常量。 (Demo)
<?php
$data = array(
array(
'id' => 0,
'title' => 'Abc'
),
array(
'id' => 2,
'title' => 'Def',
'content' => '*!'
),
array(
'id' => 0,
'title' => 'Abc'
)
);
var_dump(array_unique($data, SORT_REGULAR));
答
/* 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>";
你有看'array_unique()'? – 2013-05-05 02:46:45
如果这个数组来自一个查询,我会建议在SQL中解决这个问题,以减少sql和php之间的流量。 – 2013-05-05 02:48:39