0

我必须找到两个数组之间的差异并存储在第三个数组中。

这就是我正在做的

$newEmails=array_diff($Emails_1, $Emails_2);  

现在的问题是$Emails_1这种格式->

  array(1) { [0] => string(20) "tester@xyz.com" }

$Emails_2以这种格式出现->

array(1) { [0] => array(1) { 'email' => string(20) "tester@xyz.com" } }

我应该怎么做$Emails_2,以便它的格式与$Emails_1

PS:我不能篡改$Emails_1,只能更改$Emails_2

4

3 回答 3

0

你可以在比较之前这样做

$EmailTemp=$Emails_2[0]["email"];
$Emails_2=array();
$Emails_2[0]=$EmailTemp;
于 2013-03-27T17:34:03.627 回答
0
$Emails_1 is in this format -> array(1) { [0] => string(20) "tester@xyz.com" }

$Emails_2 comes in this format -> array(1) { [0] => array(1) { 'email' => string(20) "tester@xyz.com" } }

选项 1:为什么不先从数组中删除字符串,

$email1 = $Emails_1[0];

$email2 = $Emails_2[0]['email'];

选项 2:使您的 array_difference(..) 迭代,

function array_difference($currentEmails, $comingEmails)
{
    $newEmails = new Array();

    for ($comingEmails as $p)
    {
        if (!in_array($p['email'], $currentEmails))
        {
            $newEmails[]=$p['email'];
        }
    }

    return $newEmails;
}
于 2013-03-27T17:41:29.257 回答
0
$temp = array();
foreach($Emails_2 as $key => $value){
    $temp[$key] = $value['email'];
}
$Emails_2 = $temp;
于 2013-03-27T17:42:27.260 回答