dongtou9934 2010-09-15 16:31
浏览 7
已采纳

比较两个多维关联数组

I want to compare two arrays if an email address in array 1 exists in array 2 (here: uname1@email.com). In that case it should display that the email already exists.

$Array1 = Array
(        
[0] => Array
        (
            [username] => uname1
            [name] => fullname1
            [email] => uname1@email.com

        )
[1] => Array
        (
            [username] => uname2
            [name] => fullname2
            [email] => uname2@email.com    
        )
[2] => Array
        (
            [username] => uname3
            [name] => fullname3
            [email] => uname3@@email.com    
        )        
}   

$Array2 = Array
(    
[0] => Array
        (
            [username] => uname1
            [name] => fullname1
            [email] => uname1@email.com    
        )
}
  • 写回答

4条回答 默认 最新

  • douxiaomang5640 2010-09-15 16:38
    关注

    You might want to consider using the email as the key. Something like this:

    $a1 = array();
    foreach ($Array1 as $v) $a1[$v['email']] = $v;
    
    $a2 = array();
    foreach ($Array2 as $v) $a2[$v['email']] = $v;
    
    $intersection = array_values(array_intersect_key($a1, $a2));
    

    This yields an array that contains all the values of the first array that have an email present in the second array. You can then iterate through that array to display error messages.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?