2013-03-21 120 views
-1

關聯數組內dublicate值我有這樣的陣列刪除陣列在PHP

$arr[] = array('id' => '1', 'fn' => 'Some', 'ln' => 'Test', 'email' => '[email protected]', 'dm' => 'test'); 
$arr[] = array('id' => '2', 'fn' => 'Some2', 'ln' => 'Test2', 'email' => '[email protected]', 'dm' => 'test'); 
$arr[] = array('id' => '3', 'fn' => 'Some3', 'ln' => 'Test3', 'email' => '[email protected]', 'dm' => 'test'); 

我想刪除所有dublicate鍵$常用3有一個dublicate電子郵件,以便只有這仍然是[]:

$arr[] = array('id' => '1', 'fn' => 'Some', 'ln' => 'Test', 'email' => '[email protected]', 'dm' => 'test'); 
$arr[] = array('id' => '2', 'fn' => 'Some2', 'ln' => 'Test2', 'email' => '[email protected]', 'dm' => 'test'); 

我在這裏發現了不同的例子,但沒有人能匹配我的問題。由於

+0

[?你嘗試過什麼(http://www.whathaveyoutried.com/)參見[問諮詢】(HTTP://計算器。 com/questions/ask-advice)。 – 2013-03-21 20:05:14

回答

0
$arr = array(
    array('id' => '1', 'fn' => 'Some', 'ln' => 'Test', 'email' => '[email protected]', 'dm' => 'test'), 
    array('id' => '1', 'fn' => 'Some', 'ln' => 'Test', 'email' => '[email protected]', 'dm' => 'test'), 
    array('id' => '1', 'fn' => 'Some', 'ln' => 'Test', 'email' => '[email protected]', 'dm' => 'test'), 
    array('id' => '1', 'fn' => 'Some', 'ln' => 'Test', 'email' => '[email protected]', 'dm' => 'test'), 
    array('id' => '1', 'fn' => 'Some', 'ln' => 'Test', 'email' => '[email protected]', 'dm' => 'test') 
); 

$checkEmail = array(); // restore email values 

for($i=0;$i<count($arr);$i++){ 
    if (!in_array($arr[$i]['email'], $checkEmail)){ // check in each array if email value is unique 
     print_r($arr[$i]); // if unique then output 
    } 
    $checkEmail[$i] = $arr[$i]['email']; // save email values 
} 

OUTPUT - >

Array ([id] => 1 [fn] => Some [ln] => Test [email] => [email protected] [dm] => test) Array ([id] => 1 [fn] => Some [ln] => Test [email] => [email protected] [dm] => test) 
+0

我也想保留主陣列的索引 – user558134 2013-03-21 20:13:54

+0

@ user558134將它們還原到另一個數組中,索引將是'$ i'變量 – tnanoba 2013-03-21 20:15:35