-1

我有这个数组

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

我想删除所有具有重复电子邮件的重复键 $arr[] 以便仅保留以下内容:

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

我在这里找到了不同的例子,但没有一个能与我的问题相匹配。谢谢

4

1 回答 1

0
$arr = array(
    array('id' => '1', 'fn' => 'Some', 'ln' => 'Test', 'email' => 'email@test.com', 'dm' => 'test'),
    array('id' => '1', 'fn' => 'Some', 'ln' => 'Test', 'email' => 'other@test.com', 'dm' => 'test'),
    array('id' => '1', 'fn' => 'Some', 'ln' => 'Test', 'email' => 'email@test.com', 'dm' => 'test'),
    array('id' => '1', 'fn' => 'Some', 'ln' => 'Test', 'email' => 'email@test.com', 'dm' => 'test'),
    array('id' => '1', 'fn' => 'Some', 'ln' => 'Test', 'email' => 'other@test.com', '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
}

输出 ->

Array ( [id] => 1 [fn] => Some [ln] => Test [email] => email@test.com [dm] => test ) Array ( [id] => 1 [fn] => Some [ln] => Test [email] => other@test.com [dm] => test )
于 2013-03-21T20:11:05.613 回答