2014-08-28 122 views
0

我有2個相同的PHP數組。只有一個值不同。我想找到這個值:PHP array_diff:奇怪的行爲

var_dump(array_diff(array(
    "a" => "1", 
    "b" => "SomeString", 
    "c" => 1, // <- different value, same key 
    "d" => "4521", 
    "e" => "7546654241", 
    "f" => "78", 
    "g" => "99.999", 
    "h" => "34", 
    "i" => "http://google.com/" 
), array(
    "a" => "1", 
    "b" => "SomeString", 
    "c" => "0", // <- different value, same key 
    "d" => "4521", 
    "e" => "7546654241", 
    "f" => "78", 
    "g" => "99.999", 
    "h" => "34", 
    "i" => "http://google.com/" 
))); 

結果是array(0) { }但應該有新的"c"價值,但並非如此。當我刪除所有其他值:

var_dump(array_diff(array(
    "c" => 1 
), array(
    "c" => "0" 
))); 

我得到了我想要array(1) { ["c"]=> int(1) }

我沒有低估它。爲什麼這樣?

+1

也許這會對你更好http://php.net/manual/en/function.array-diff-assoc.php – RST 2014-08-28 16:41:02

回答

4

和array_diff()從所述第一陣列匹配到第二

returns the values in array1 that are not present in any of the other arrays.

第一陣列中的關鍵c價值1存在第二陣列爲關鍵a的值,所以沒有區別有沒有和array_diff()將識別

也許使用array_diff_assoc()將GVE你結果你實際上試圖得到...... http://ideone.com/xHCVfF

+0

你的意思是'和array_diff_assoc()'?爲什麼OP沒有得到他們期望的好解釋。 – 2014-08-28 16:41:45

+0

@Mike - 我只是將array_diff_assoc輸入到ideone.com :) – 2014-08-28 16:43:09