2012-05-25 78 views
0

我想比較下面的2個數組,並找出差異。鍵「主人」和「公司」的值是不同的,但是當我比較這些數組時,它表示只有「公司」值是不同的。但是,當我創建2個只有一個鍵/值對「主人」的新陣列時,它可以正常工作。我犯了一些錯誤嗎?這個簡單的代碼有什麼問題?

<?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) 
+1

的輸出是什麼? – AlphaMale

+0

添加了輸出。 –

+0

也添加了你想要的輸出。意味着你想要的輸出是什麼? – AlphaMale

回答

1

如果要在關聯數組之間進行區分,以便鍵值對非常重要,而不僅僅是值,請使用array_diff_assoc而不是array_diff

+0

它返回了一些錯誤的結果,我的意思是它返回的幾列不是不同的前綴:「no_of_employees」,「annual_revenue」,「created_by」 「CREATED_TIME」。此鍵的值完全相同。爲什麼會這樣? –

+0

看起來你的一些鍵正在使用空格,有些使用下劃線。 –

+0

嘗試複製上面的代碼...沒有下劃線。它也返回錯誤的結果。 –

1

你錯誤地理解它。

array_diff文檔頁面說

返回包含所有從ARRAY1不在 存在於任何其他陣列的條目的陣列。

Amit Patil存在於第二陣列因此它返回只有一個值,它是在SAM和它只有一個值,它不存在於第二陣列。

+0

Shakti,謝謝你的解釋,現在我明白了addta_diff()和array_diff_assoc()之間的差異 –

1

array_diff()返回互補值。所以你可以這樣做:

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

這樣它會工作。

1

發生這種情況是因爲「array_diff」函數返回第一個數組中任何鍵的值,而不是第二個數組中的任何鍵都不會發生。

lead_owner「Amit Patil」不等於第二個數組lead_owner,但等於第二個數組的created_by和modified_by鍵。

您應該爲此使用「array_diff_assoc」。

1

試試這個,看看,這應該工作

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