检查两个阵列是否共享任何公共密钥

检查两个阵列是否共享任何公共密钥

问题描述:

What's the best way to check if at least one key of an array exists in another?

This means, I want to find out if two arrays share any keys in common, and return true if this is so for at least one key.

$required = array('red', 'blue');

$colours = array(
    'red' => 'one',
    'blue' => 'two',
    'green' => 'three'
);

If the red key was found in the $colours array, it would still return true, just as it would if they both were present.

I have looked at examples, but they seem to check if ALL keys are present, or checking if just one key exists. I could use array_key_exists many times and use if (.. || ..), but I would like to use another array.

检查数组中是否存在至少一个数组键的最佳方法是什么? p>

这意味着,我想知道两个数组是否共用任何共同的键,如果对于至少一个键,则返回true。 p>

  $  required = array('red','blue'); 
 
 $ colors = array(
'red'=>'one',
'blue'=>'two',
'green  '=>'三'
); 
  code>  pre> 
 
 

如果在 $ colors中找到 red code>键 代码>数组,它仍然会返回true,就像它们都存在一样。 p>

我查看了示例,但它们似乎检查是否存在所有键,或者检查 如果只存在一个密钥。 我可以多次使用array_key_exists并使用 if(.. || ..) code>,但我想使用另一个数组。 p> div>

Use array_intersect_key. If the resulting array has more than 0 items, then they had keys in common.

Since $required only contains values, you can use array_flip to create an array with colour keys from it.

When you combine all of it, you can write something like this:

$required = array('red', 'blue');

$colours = array(
    'red' => 'one',
    'blue' => 'two',
    'green' => 'three'
);

if (count($intersection = array_intersect_key($colours, array_flip($required))) > 0)
{
  echo 'They have the keys ' . implode(',', array_keys($intersection)) . ' in common';
}

You can even take advantage from the fact that array_intersect_key returns the intersecting keys with the values from the first array, that is, the array that is passed in the first parameter.

So, if you want to display the keys and values, this is possible as well (split up a little, to make it slightly more readable).

$required = array('red', 'orange', 'green');

$colours = array(
    'red' => 'one',
    'blue' => 'two',
    'green' => 'three'
);

// Convert required colours to keys
$requiredKeys = array_flip($required);

// Get the intersection. Pass in `$colours` first if you want the values too.
$intersection = array_intersect_key($colours, $requiredKeys);

if (count($intersection) > 0)
{
  // Get and display the matching keys.
  $matchingKeys = array_keys($intersection);
  echo 'They have the keys "' . implode('","', $matchingKeys) . '" in common. ';

  // And the corresponding values.
  echo 'Relating to the values "' . implode('","', $intersection) . '". ';
}

This is sample code from here

<?php
$array1 = array('blue'  => 1, 'red'  => 2, 'green'  => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan'   => 8);

var_dump(array_intersect_key($array1, $array2));
?>

This should give you the intersection of the 2 arrays which have common keys.

array_intersect_key — Computes the intersection of arrays using keys for comparison

usage

array array_intersect_key ( array $array1 , array $array2 [, array $... ] )

array_intersect_key() returns an array containing all the entries of array1 which have keys that are present in all the arguments.