给定以下大海捞针:
$haystack = array(
'foo' => array(
1 => 'one',
2 => 'two',
3 => 'three',
),
);
$needle = array(
'foo' => array(
1 => 'one',
2 => 'two',
),
);
我想检查针的所有嵌套键值对是否都出现在草堆中(就像上面的例子中一样),同时忽略草堆中可能存在的任何其他键值对(就像例子中的$haystack['foo'][3]
)。
SO上有很多类似的问题,但我还没有找到这个特定用例的解决方案。有没有标准的PHP函数(组合)可以做到这一点?什么是最优雅的解决方案?
[更新]
我还没有弄清楚阵列可能并不总是具有相同的深度。此外,阵列中的元素的键每次都可能不同。
array_intersect()
将告诉您匹配的值。只要确保它与您的$needle
相匹配即可。
echo $needle['foo'] === array_intersect($needle['foo'], $haystack['foo']);
这就是我自己想出的。它有效,但似乎比它应该的更复杂…
/**
* Helper function which recursively checks if the key-value pairs in one array
* are all present in another array. If all key-value pairs in the needle are
* present in the haystack, and the haystack also contains additional items,
* the check wil still pass.
*
* @param array $needle
* The array with the key-value pairs to look for.
* @param array $haystack
* The array in which to look for the key-value pairs.
* @return bool
* TRUE if all key-value pairs of the needle occur in the haystack. FALSE if
* one or more keys or values are missing or different.
*/
function array_contains(array $needle, array $haystack) {
// First, check if needle and haystack are identical. In that case it's easy.
if ($needle === $haystack) {
return TRUE;
}
foreach ($needle as $key => $value) {
// If the key does not occur in the haystack, we're done.
if (!isset($haystack[$key])) {
return FALSE;
}
// If the value is an array...
if (is_array($value)) {
// ...see if the counterpart in $haystack is an array too...
if (!is_array($haystack[$key])) {
return FALSE;
}
// ...and if so, recurse.
if (array_contains($value, $haystack[$key]) == FALSE) {
return FALSE;
}
}
// If the values are not arrays and not the same, the check fails.
else if ($value != $haystack[$key]) {
return FALSE;
}
}
// If we still didn't fail, all tests have passed.
return TRUE;
}