百科狗-知识改变命运!
--

array_diff_key() - 使用键名比较计算数组的差集 - php 数组函数

梵高1年前 (2023-11-21)阅读数 10#技术干货
文章标签数组

array_diff_key()

(PHP 5 >= 5.1.0, PHP 7)

使用键名比较计算数组的差集

说明

array_diff_key(array $array1,array $array2[,array $...]): array

根据$array1中的键名和$array2进行比较,返回不同键名的项。本函数和array_diff()相同只除了比较是根据键名而不是值来进行的。

参数

$array1

从这个数组进行比较

$array2

针对此数组进行比较

更多比较数组

返回值

array_diff_key() - 使用键名比较计算数组的差集 - php 数组函数

array_diff_key()返回一个数组,该数组包括了所有出现在$array1中但是未出现在任何其它参数数组中的键名的值。

范例

Example #1array_diff_key()

key => value对中的两个键名仅在(string)$key1 ===(string)$key2时被认为相等。换句话说,执行的是严格类型检查,因此字符串的表达必须完全一样。

以上例程会输出:

array(2) {
  ["red"]=>
  int(2)
  ["purple"]=>
  int(4)
}

注释

Note:

注意本函数只检查了多维数组中的一维。当然,可以用array_diff_key($array1[0],$array2[0]);来检查更深的维度。

参见

  • array_diff() 计算数组的差集
  • array_udiff() 用回调函数比较数据来计算数组的差集
  • array_diff_assoc() 带索引检查计算数组的差集
  • array_diff_uassoc() 用用户提供的回调函数做索引检查来计算数组的差集
  • array_udiff_assoc() 带索引检查计算数组的差集,用回调函数比较数据
  • array_udiff_uassoc() 带索引检查计算数组的差集,用回调函数比较数据和索引
  • array_diff_ukey() 用回调函数对键名比较计算数组的差集
  • array_intersect() 计算数组的交集
  • array_intersect_assoc() 带索引检查计算数组的交集
  • array_intersect_uassoc() 带索引检查计算数组的交集,用回调函数比较索引
  • array_intersect_key() 使用键名比较计算数组的交集
  • array_intersect_ukey() 用回调函数比较键名来计算数组的交集
To return the unique elements (those with a key that exists only once in either array but not in both) try:
function array_unique_diff ($array1, $array2)
{
 array_merge(array_diff_key($array1, $array2), array_diff_key($array2, $array1));
}
Example:
$array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan'  => 8);
 array_diff_key($array1, $array2)
returns
 array ( 'red' => 2, 'purple' => 4 ) 
 array_diff_key($array2, $array1)
returns
 array ( 'yellow' => 7, 'cyan' => 8, )
 array_unique_diff($array1, $array2);
 
returns
 array ( 'red' => 2, 'purple' => 4, 'yellow' => 7, 'cyan' => 8, )
I needed something a little different where maybe even the keys in multidimensional arrays don't match up. Setting $assoc to false will cause only to check for missing keys, otherwise it compares values as well. This was also based on '2ge at 2ge dot us' function 
The PHP4 version below works only unidirectionally. If you switch the arrays around i.e. (ar2, ar1) you get different results than (ar1, ar2).
The recursive function suggested by '2ge at 2ge dot us' will provide you with empty arrays if there's no diff. 
This variant of the function cleans up empty arrays and fixes a bug in the first suggested version. It works 100%
. 
Seems to be a great function, especially for n-dimensions arrays. The only problem is that I cannot find it in php 5.0.3 and 5.0.4. Does it really exist ?! :(
[20:27:05][maxence@conurb] ~/test2/php-5.0.4$ grep PHP_FUNCTION * -r | grep -i array_diff_key 
[20:27:09][maxence@conurb] ~/test2/php-5.0.4$
Improved recursive version.

An up to date version is maintained at https://github.com/gajus/flow/blob/master/flow.inc.php#L337.
Well, you could implement in the code something more powerfull: 
http://www.php.net/manual/en/function.array-diff.php#31364
Here's a simple function that returns true if all keys in the first array are found in the second array, and false if they aren't.
function same_keys ($a1, $a2) {
  $same = false;
  if (!array_diff_key($a1, $a2)) {
    $same = true;
    foreach ($a1 as $k => $v) {
      if (is_array($v) && !same_keys($v, $a2[$k])) {
        $same = false;
        break;
      }
    }
  }
  return $same;
}
To check if two arrays have the same structure, ignoring values, execute the function twice, the second time with the arguments reversed.
One more alternative variant :) 
you can use this function for return the difference of two array ! 
Hello, if you need diff key of n-dimensional arrays here is nice solution:

it will print everything, what is missing in $a2.

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)