I was trying to clarify that one array might have more values than the other. It's entirely different to say that $value2
has all of the elements in $value1
than to say that $value1
has all the elements in $value2
which is not the same thing as $value1
AND $value2
contain exactly the same elements.
More precisely, $value2
might contain every item in $value1
but also extra values that are not in $value1
.
And you didn't answer my question as to whether you are checking associative arrays (i.e., arrays with textual key names) or whether you are checking just array values or numerically indexed arrays.
array_diff
is very handy if you don't care about the array keys:
$v1 = [
"key1" => "value 1",
"key2" => "value 2",
];
$v2 = [
"key1" => "value 1",
"key3" => "value 3",
"key2" => "value 2",
];
$v3 = [
"alt1" => "value 1",
"alt2" => "value 2",
"alt3" => "value 3",
];
var_dump(array_diff($v1, $v2));
var_dump(array_diff($v2, $v1));
var_dump(array_diff($v2, $v3));
var_dump(array_diff($v3, $v2));
output:
array(0) {
}
array(1) {
["key3"]=>
string(7) "value 3"
}
array(0) {
}
array(0) {
}
If you want to check and make sure that all of the array keys in $value1
are also set in $value2
then you could do something like this:
$result = true; // assume everything is cool
foreach(array_keys($v1) as $key) {
if (!isset($v2[$key]) || $v1[$key] !== $v2[$key]) {
$result = false; // missing or ummatched element!
}
}
var_dump($result);