ButtressCoral
ButtressCoral

Reputation: 113

Check if two arrays share any common keys

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.

Upvotes: 2

Views: 2457

Answers (4)

GolezTrol
GolezTrol

Reputation: 116110

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) . '". ';
}

Upvotes: 4

Tushar Gupta
Tushar Gupta

Reputation: 15923

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.

Upvotes: 1

Jay
Jay

Reputation: 2686

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.

Upvotes: 2

Related Questions