2

I am trying to count the matches between expected and actual in a PHP array, I have this...

$array = array(
    "item" => array(
        'expected' => array(
            '1' => 25,
            '2' => 4,
            '3' => 4,
        ),
        'color' => 'red',
        'actual' => array(
            '1' => 25,
            '2' => 4,
            '3' => 3,
        ),
    ),
);

foreach ($array as $key => $arrayItem) {

    $matches = array (
        'matches'  => count ( array_intersect ( $arrayItem['expected'], $arrayItem['actual'] ) ),
    );

}

echo "Matches = " . $matches['matches'];

I am expecting this to return 2 but it is actually returning 3. If I change the values like in the example below then it does work...

$array = array(
    "item" => array(
        'expected' => array(
            '1' => 25,
            '2' => 84,
            '3' => 4,
        ),
        'color' => 'red',
        'actual' => array(
            '1' => 25,
            '2' => 84,
            '3' => 3,
        ),
    ),
);

foreach ($array as $key => $arrayItem) {

    $matches = array (
        'matches'  => count ( array_intersect ( $arrayItem['expected'], $arrayItem['actual'] ) ),
    );

}

echo "Matches = " . $matches['matches'];

Anyone any ideas why the top version is not giving me the expected result?

4
  • 1
    In first approach, it matches 2nd 4 of expected with 2nd 4 of actual. Commented Feb 13, 2020 at 10:21
  • 2
    From the manual: array_intersect: "Returns an array containing all of the values in array1 whose values exist in all of the parameters. " Commented Feb 13, 2020 at 10:27
  • 2
    Perhaps you want array_intersect_assoc? Commented Feb 13, 2020 at 10:29
  • 1
    I think array_intersect_assoc might be the answer to this one. Reading up on it now Commented Feb 13, 2020 at 10:31

2 Answers 2

3

Because it returns an array containing all values in array1 whose values exist in all of the parameters.

array_intersect(array $array1, array $array2[, array $... ]): array

https://www.php.net/manual/en/function.array-intersect.php

Maybe you can see it clearly from this perspective:

var_dump(array_intersect([25, 4, 4, 4], [25, 4, 3])); // [25, 4, 4, 4] 
// because the number `4` is in the second array!

var_dump(array_intersect([25, 4, 3], [25, 4, 4, 4])); // [25, 4]
Sign up to request clarification or add additional context in comments.

Comments

2

The count is actually correct.

It doesn't happen in your second example because you use the numbers 84 and 4, but essentially here are the matches:

$arrayItem['expected'][1] matches with $arrayItem['actual'][1] (25 and 25)

$arrayItem['expected'][2] matches with $arrayItem['actual'][2] (4 and 4)

$arrayItem['expected'][3] matches with $arrayItem['actual'][2] (4 and 4)

The count of 3 is correct.


You can test this by changing your code to the following:

$matches = array(
    'matches' => array_intersect ($arrayItem['expected'], $arrayItem['actual'])
);

var_dump($matches);

Here you'll see this output:

array(1) {
    ["matches"] => array(3) {
        [1]=> int(25) 
        [2]=> int(4) 
        [3]=> int(4)
    }
}

2 Comments

$arrayItem['expected'][2] and $arrayItem['expected'][3] are counting as a match Not true. See demo
Number 4 is met twice in expected, that's why 4 is checked twice in actual and gets a match of course. Nothing is matched with itself.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.