简体   繁体   English

这个简单的代码有什么问题?

[英]What's wrong with this simple code?

I want to compare the 2 arrays below and find the differences. 我想比较下面的2个数组并找到不同之处。 The values for keys "lead owner" and "company" are different, but when I compare these arrays, it says only "company" values are different. 键“ lead owner”和“ company”的值不同,但是当我比较这些数组时,它说只有“ company”值不同。 But when I create 2 new arrays with only one key/value pair for "lead owner" it works properly. 但是,当我为“潜在客户所有者”创建只有一个键/值对的2个新数组时,它可以正常工作。 Am I making some mistake? 我在犯错吗?

<?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));
?>

Output is as below 输出如下

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

You are understanding it wrongly. 您误会了。

The documentation page of array_diff says array_diff的文档页面说

Returns an array containing all the entries from array1 that are not present in any of the other arrays. 返回一个数组,其中包含array1中所有其他数组中不存在的所有条目。

But the Amit Patil is present in the second array hence it is returning only one value which is SAM and It is only one value which is not present in second array. 但是Amit Patil存在于第二个数组中,因此它仅返回一个值为SAM值,并且仅返回一个值不存在于第二个数组中。

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

array_diff() returns complementary values. array_diff()返回互补值。 So you can do it like this: 因此,您可以这样做:

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

This way it will work. 这样,它将起作用。

It happens because the "array_diff" function returns the values that occurs for any key in the first array and do not occurs for none of the keys in the second array. 发生这种情况是因为“ array_diff”函数返回的值出现在第一个数组中的任何键上,而没有出现在第二个数组中的任何键上。

The lead_owner "Amit Patil" is not equal to the second array lead_owner, but is equal to the created_by and modified_by keys of the second one. lead_owner“ Amit Patil”不等于第二个数组lead_owner,但等于第二个数组的created_by和Modify_by键。

You should use "array_diff_assoc" for this purpose. 为此,您应该使用“ array_diff_assoc”。

try this and see, this should work 试试这个,看看,这应该工作

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM