I don't know what you are trying to do with a foreach
loop. Are you saying every array will have 2 child arrays
or every child array will have 5 values
? Regardless, I hope this helps you!
I would use some of these:
array_intersect
array_intersect_assoc
array_diff
array_diff_assoc
Code:
<?php
$array1 = [
[
'A' => 'GB',
'B' => 'Harry',
'C' => 'British Army',
'D' => 'Eton College',
'E' => 'Cressida Bonas',
],
[
'A' => 'GB',
'B' => 'William',
'C' => 'Royal Air Force',
'D' => 'Eton College',
'E' => 'Catherine Middleton',
]
];
// What Values are in common
$result1 = array_intersect_assoc($array1[0], $array1[1]);
print_r($result1);
$array2 = [
[
'A' => 'GB',
'B' => 'Harry',
'C' => 'British Army',
'D' => 'Eton College',
'E' => 'Cressida Bonas',
],
[
'A' => 'GB',
'B' => 'James',
'C' => 'British Army',
'D' => 'Millfield',
'E' => 'Unknown',
]
];
// What values are different
$result2 = array_diff_assoc($array2[0], $array2[1]);
print_r($result2);
// A Way to check numerically
$perfectMatch = 5;
$intersect = array_intersect_assoc($array1[0], $array1[1]);
$intersectCount = count($intersect);
if ($intersectCount != $perfectMatch) {
echo "<br> Not Every value matches.";
} else {
echo "<br> Perfect Match!";
}
To compare the full arrays $array1
and $array2
you can do:
<?php
// (With array code above)
$c1 = count($array1);
$c2 = count($array2);
if ($c1 != $c2) {
echo "<br>Array Children must be the same";
}
$result = [];
for ($i = 0; $i < $c1; $i++) {
$in_common = array_intersect($array1[$i], $array2[$i]);
$result[] = count($intersect);
}
print_r($result);