array_diff_assoc() - 带索引检查计算数组的差集 - php 数组函数
array_diff_assoc()
(PHP 4 >= 4.3.0, PHP 5, PHP 7)
带索引检查计算数组的差集
说明
array_diff_assoc(array $array1,array $array2[,array $...]): arrayarray_diff_assoc()返回一个数组,该数组包括了所有在$array1中但是不在任何其它参数数组中的值。注意和array_diff()不同的是键名也用于比较。
参数
$array1从这个数组进行比较
$array2被比较的数组
更多被比较的数组
返回值
Returns anarraycontaining all the values from$array1that are not present in any of the other arrays.
范例
Example #1array_diff_assoc()例子
上面的例子中可以看到键值对"a"=>"green"在两个数组中都有,因此不在本函数的输出中。与此不同,键值对0 =>"red"出现在输出中是因为第二个参数中的"red"的键名是1。
以上例程会输出:
Array ( [b] => brown [c] => blue [0] => red )
Example #2array_diff_assoc()example
键值对key => value中的两个值仅在(string)$elem1 ===(string)$elem2时被认为相等。也就是说使用了严格检查,字符串的表达必须相同。
以上例程会输出:
Array ( [0] => 0 [1] => 1 )
注释
Note:注意本函数只检查了多维数组中的一维。当然可以用array_diff_assoc($array1[0],$array2[0]);检查更深的维度。
Note:使用更多的键比较相似数组时,确保你传入参数的顺序是正确的。新的数组应该是在列表里的第一个。
参见
array_diff()
计算数组的差集array_diff_uassoc()
用用户提供的回调函数做索引检查来计算数组的差集array_udiff_assoc()
带索引检查计算数组的差集,用回调函数比较数据array_udiff_uassoc()
带索引检查计算数组的差集,用回调函数比较数据和索引array_intersect()
计算数组的交集array_intersect_assoc()
带索引检查计算数组的交集
The array_diff_assoc_array from "chinello at gmail dot com" (and others) will not work for arrays with null values. That's because !isset is true when an array key doesn't exists or is set to null. (sorry for the changed indent-style) And here an example (note index 'b' in the output): array(1) { ["c"]=> array(1) { ["d"]=> NULL } } array(2) { ["b"]=> NULL ["c"]=> array(1) { ["d"]=> NULL } }
If you're looking for a true array_diff_assoc, comparing arrays to determine the difference between two, finding missing values from both, you can use this along with array_merge. $a = array('a'=>1,'b'=>2,'c'=>3); $b = array('a'=>1,'b'=>2,'d'=>4); print_r(array_diff_assoc($a,$b)); // returns: array ( [c] => 3 ) print_r(array_diff_assoc($b,$a)); // returns array ( [d] => 4 ) print_r(array_merge(array_diff_assoc($a,$b),array_diff_assoc($b,$a))); // returns array ( [c] => 3 [d] => 4 )
The direction of the arguments does actually make a difference: echoes: Array ( ) Array ( [g] => g )
an earlier post for recursive array_diff_assoc failed because isset returned false on an array element containing a null value. I updated the code so it compares null values too.
The following will recursively do an array_diff_assoc, which will calculate differences on a multi-dimensional level. This not display any notices if a key don't exist and if error_reporting is set to E_ALL: [NOTE BY danbrown AT php DOT net: This is a combination of efforts from previous notes deleted. Contributors included (Michael Johnson), (jochem AT iamjochem DAWT com), (sc1n AT yahoo DOT com), and (anders DOT carlsson AT mds DOT mdh DOT se).]
Hi all, For php versionsTo diff between n-dimensional array, juste use this :To unset elements in an array if you know the keys but not the values, you can do: Of course this makes most sense if $b has many elements or is dynamically generated.NOTE: the diff_array also removes all the duplicate values that match to the values in the second array:array_diff_assoc can also be used to find the duplicates in an array Note: The index of the $dups is not in strict sequential order as expected by C programmer.A quite simple (yet not very efficient) way to compare the first level of arrays which have values that are not strings: array_map('unserialize',array_diff_assoc(array_map('serialize',$arr1),array_map('serialize',$arr2))) Might be useful for debugging (that's what I use it for).Recursive implementation accepting multiple n-level-arrays as parameters: The other attempt was cleaner but didn't work for all cases.Works more like the original function:there is a functiont that i searched long enough now i have created it so someone else to find it if he need it ;]array_diff_assoc will fail, if a value is something that can not be converted to a string.Yet another recursive implementation, without if-else hell and with multiple parameters just like the original.
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!