2013-05-05 226 views
3

的数组下面是我的工作对数组的一个例子:删除重复阵列,从阵列

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比较和删除重复。

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

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?

+0

你有看'array_unique()'? – 2013-05-05 02:46:45

+0

如果这个数组来自一个查询,我会建议在SQL中解决这个问题,以减少sql和php之间的流量。 – 2013-05-05 02:48:39

回答

4

试试这个:

<?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); 
?> 
0

这是简单的实现(和快,因为它的内部)与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)); 
0
/* 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>";