我有两个相同的PHP数组。只有一个值不同。我想找到这个值:
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"
值,但不是。{n}。当我删除所有其他值时:
var_dump(array_diff(array(
"c" => 1
), array(
"c" => "0"
)));
我得到了我想要的array(1) { ["c"]=> int(1) }
。
我不会理解它。为什么会这样?
答案 0 :(得分:4)
array_diff()将 值 与第一个数组匹配到第二个
returns the values in array1 that are not present in any of the other arrays.
第一个数组 中的键1
的值c
存在于第二个数组中作为键{{1}的值因此,array_diff()将识别
也许使用array_diff_assoc()会让你得到你真正想要获得的结果.... http://ideone.com/xHCVfF
答案 1 :(得分:2)
我尝试了http://php.net/manual/en/function.array-diff-assoc.php,我得到了
array (size=1)
'c' => int 1
至于使用array_diff,它会比较值...