PHP array_diff_key
1.函数的作用:通过键值来比较数组的差集。
2.函数的参数:
@params array $array1 被比较的数组
@params array $array2 比较的数组
@params array $array3 比较的数组
...
3.注意点:
1)比较方法: (string)element1 === (string)element2
4.例子:
1 $array1 = [1 => 'a',2 => 'b','c']; 2 $array2 = ['1' => 'a','c']; 3 print_r(array_diff_key($array1,$array2));
输出:
1 $array1 = ['joiner1' => 'swimming','joiner2' => 'jogging','joiner3' => 'basketball','joiner4' => 'football']; 2 $array2 = ['joiner1' => 'chess','joiner3' => 'LandLords']; 3 $array3 = ['joiner2' => 'singing']; 4 5 echo 'Everyone has two activities!The sports activity is required!So,The last best activity will be assigned to who?'; 6 echo "\n" .'He is named '; 7 print_r(key(array_diff_key($array1,$array2,$array3))); 8 echo "!";
输出:
1 /* 2 * 求两个数组交集的补集 3 * 4 * http://php.net/manual/zh/function.array-diff-key.php 5 */ 6 function array_unique_diff ($array1, $array2) 7 { 8 array_merge(array_diff_key($array1, $array2), array_diff_key($array2, $array1)); 9 }
1 /* 2 * 得到数组2缺失的东西
3 * http://php.net/manual/zh/function.array-diff-key.php
4 */ 5 function n_array_diff ($a1, $a2) { 6 foreach($a1 as $k => $v) { 7 $r[$k] = is_array($v) ? n_array_diff($a1[$k], $a2[$k]) : array_diff_key($a1, $a2); 8 } 9 return $r; 10}
学习记录,方便复习