2013-05-05 260 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>";