从2个关联数组返回匹配键(不同值),

时间:2015-05-07 15:40:47

标签: php arrays associative-array

所以我们假设我们有两个数组:

$x = array(
   "id" => 12,
   "name" => "Joe",
   "surname" => "Murphy",
   "age" => 52
);

$y = array(
   "id" => 12,
   "name" => "Joe",
   "surname" => "Murphy",
   "age" => 53
);

function key_compare_func($key1, $key2)
{
    if ($key1 == $key2)
        return 0;
    else if ($key1 > $key2)
        return 1;
    else
        return -1;
}

var_dump(array_intersect_ukey($x, $y, 'key_compare_func')); 

这将返回$ x

中的所有匹配元素
array(4) { 
["id"]=> int(12) 
["name"]=> string(3) "Joe" 
["surname"]=> string(6) "Murphy" 
["age"]=> int(52) 
}

我只需要["age"]=> int(52)

我看了这些,但似乎没有提供这种功能,除非我错过了一些东西:

  

array_ udiff_ ASSOC,   array_ uintersect_ ASSOC,   array_ uintersect_ uassoc,
  array_ udiffarray_ uintersect,   array_ udiff_ uassoc

1 个答案:

答案 0 :(得分:1)

http://php.net/manual/en/function.array-diff-assoc.php

var_dump(array_diff_assoc($x, $y));