0

我想比较下面的 2 个数组并找出差异。键“lead owner”和“company”的值不同,但是当我比较这些数组时,它说只有“company”值不同。但是,当我为“主要所有者”创建两个只有一个键/值对的新数组时,它可以正常工作。我犯了一些错误吗?

<?php

$arr1 = Array
    (
        "leadid" => "418176000000069007",
        "smownerid" => "418176000000047003",
        "lead owner" => "Amit Patil",
        "company" => "SAM",
        "first name" => "Test",
        "last name" =>"Lead1",
        "designation" => "call",
        "email" => "",
        "phone" => "958",
        "fax" => "",
        "mobile" => "",
        "website" => "www.infosys.con",
        "lead source" => "Cold Call",
        "lead status" => "Contact in Future",
        "industry" => "None",
        "no of employees" => "45000",
        "annual revenue" => "0",
        "rating" => "Active",
        "smcreatorid" => "418176000000047003",
        "created by" => "Amit Patil",
        "modifiedby" => "418176000000047003",
        "modified by" => "Amit Patil",
        "created time" => "2012-04-05 19:58:00",
        "modified time" => "2012-05-02 08:51:08",
        "street" => "",
        "city" => "",
        "state" => "",
        "zip code" => "",
        "country" => "",
        "description" => "",
        "skype id" => "",
        "email opt out" => "false",
        "salutation" => "Mr.",
        "secondary email" => ""
        );

$arr2 = Array
    (
        "leadid" => "418176000000069007",
        "smownerid" => "418176000000047003",
        "lead owner" => "Amit aaa",
        "company" => "SAM A",
        "first name" => "Test",
        "last name" => "Lead1",
        "designation" => "call",
        "email" => "",
        "phone" => "958",
        "fax" => "",
        "mobile" => "",
        "website" => "www.infosys.con",
        "lead_source" => "Cold Call",
        "lead_status" => "Contact in Future",
        "industry" => "None",
        "no_of_employees" => "45000",
        "annual_revenue" => "0",
        "rating" => "Active",
        "smcreatorid" => "418176000000047003",
        "created_by" => "Amit Patil",
        "modifiedby" => "418176000000047003",
        "modified_by" => "Amit Patil",
        "created_time" => "2012-04-05 19:58:00",
        "modified_time" => "2012-05-02 08:51:08",
        "street" => "",
        "city" => "",
        "state" => "",
        "zip_code" => "0",
        "country" => "",
        "description" => "",
        "skype_id" => "",
        "email_opt_out" => "false",
        "salutation" => "Mr.",
        "secondary_email" => ""
        );

$arr3 = array("lead owner" => "Amit Patil");
$arr4 = array("lead owner" => "Amit aaa");

print_r(array_diff($arr1,$arr2));
echo "<br>";
print_r(array_diff($arr3,$arr4));
?>

输出如下

Array ( [company] => SAM )
Array ( [lead owner] => Amit Patil ) 
4

5 回答 5

1

你理解错了。

array_diff的文档页面说

返回一个数组,其中包含 array1 中不存在于任何其他数组中的所有条目。

但是Amit Patil它存在于第二个数组中,因此它只返回一个值,SAM并且它只是一个不存在于第二个数组中的值。

于 2012-05-25T05:30:55.020 回答
1

如果您想在关联数组之间进行区分,以便键值对很重要,而不仅仅是值,请使用array_diff_assoc,而不是array_diff

于 2012-05-25T05:31:39.533 回答
1

array_diff()返回互补值。所以你可以这样做:

   array_diff(array_merge($arr1, $arr2), array_intersect($arr1, $arr2));

这样它就会起作用。

于 2012-05-25T05:33:53.600 回答
1

发生这种情况是因为“array_diff”函数返回出现在第一个数组中的任何键的值,而不会出现在第二个数组中的任何键上。

Lead_owner“Amit Patil”不等于第二个数组lead_owner,而是等于第二个数组的created_by 和modified_by 键。

为此,您应该使用“array_diff_assoc”。

于 2012-05-25T05:35:26.580 回答
1

试试这个,看看,这应该工作

print_r(array_diff_assoc($arr1,$arr2));
echo "<br>";
print_r(array_diff_assoc($arr3,$arr4));
于 2012-05-25T05:35:43.800 回答