如何修剪数组格式?

时间:2013-03-27 17:30:03

标签: php arrays

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

这就是我为此做的事情

$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

3 个答案:

答案 0 :(得分:0)

你可以在比较之前做到这一点

$EmailTemp=$Emails_2[0]["email"];
$Emails_2=array();
$Emails_2[0]=$EmailTemp;

答案 1 :(得分: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;
}

答案 2 :(得分:0)

$temp = array();
foreach($Emails_2 as $key => $value){
    $temp[$key] = $value['email'];
}
$Emails_2 = $temp;