Given the following haystack and needle:
$haystack = array(
'foo' => array(
1 => 'one',
2 => 'two',
3 => 'three',
),
);
$needle = array(
'foo' => array(
1 => 'one',
2 => 'two',
),
);
I want to check if all nested key-value pairs of the needle occur in the haystack (like it does in the example above), while ignoring any additional key-value pairs that may exist in the haystack (like $haystack['foo'][3]
in the example).
There are many similar questions on SO but I haven't found a solution for this specific use case. Is there a (combination of) standard PHP functions to do this? What is the most elegant solution?
[update]
I didn't make clear yet that the arrays may not always have the same depth. Also, the keys of the elements in the arrays may be different every time.