Question

I have two two-dimensional arrays I'd liked to compare to one another:

$array1
Array
(
    [0] => Array
        (
            [A] => GB
            [B] => Harry
            [C] => British Army
            [D] => Eton College
            [E] => Cressida Bonas
        )

    [1] => Array
        (
            [A] => GB
            [B] => William
            [C] => Royal Air Force
            [D] => Eton College
            [E] => Catherine Middleton
        )

)

$array2
Array
(
    [0] => Array
        (
            [A] => GB
            [B] => Harry
            [C] => British Army
            [D] => Eton College
            [E] => Cressida Bonas
        )

    [1] => Array
        (
            [A] => GB
            [B] => James
            [C] => British Army
            [D] => Millfield
            [E] => Unknown
        )

)

And produce a boolean true/false results array as follows if any of the child array values differ:

$results_array
Array
(
    [0] => Array
        (
            [0] => true
        )

    [1] => Array
        (
            [0] => false      
        )

)

UPDATE: Both arrays will always have the same length parent arrays (but the child arrays values may vary in length).

I can't wrap my head around how to use foreach in a recursive manner to get the results array.

Any general ideas or advice?

Was it helpful?

Solution

You could do using == operator. Passing the first array as key-array ($k=>$arr) [since the value is another array]. Now compare the two arrays using the simple == comparison operator. You can even perform strict matching using the === operator.

<?php
$arr1=array(0=>array('A'=>'GB','B'=>'Harry','C'=>'British Army'),1=>array('A'=>'GB','B'=>'William','C'=>'Royal Air Force'));
$arr2=array(0=>array('A'=>'GB','B'=>'Harry','C'=>'British Army'),1=>array('A'=>'GB','B'=>'James','C'=>'British Army'));

foreach($arr1 as $k=>$arr)
{
    $resarr[][$k]=($arr==$arr2[$k])? "true" : "false";
}
print_r($resarr);

Demo

OUTPUT :

Array
(
    [0] => Array
        (
            [0] => true
        )

    [1] => Array
        (
            [1] => false
        )

)

OTHER TIPS

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);

You can just use array_map(), assuming both arrays are of the same length:

$result = array_map(function($a, $b) {
    return [$a == $b]; // create single element boolean array
}, $array1, $array2);

You can use either == or ===; the former (==) will yield true if both arrays have the same key and value pairs, whereas the latter (===) will only yield true if the keys of both arrays are also in the same order.

Some answers use the == operator; however that will not work if the arrays do not have the same key=>value matchings; Try this:

$results = array();
foreach ($array1 as $i => $subarray)
{
    /* No need to proceed if subarray[i] does not exist in the second array */
    if(!is_array($array2[$i]))
    {
        $results[$i] = false;
        continue;
    }        
    $results[$i] = (count(array_diff($array1[$i], $array2[$i])) == 0) ? true : false;
}

This solution is a bit slower than using the == comparison operator. However using the == will not work for a case like:

$array1 => array(1 => "John", 2 => "James");
$array2 => array(1 => "James", 2 => "John");

Here, the keys are not the same, but the values are duplicated. Solutions using == will return false when comparing these arrays.

$array1 == $array2     // False
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top