Jeffrey Chan
Jeffrey Chan

Reputation: 93

Count the differences between two flat, associative arrays

Array ( [34] => A [35] => B [36] => B [37] => C ) //This is the Answer

Compares

Array ( [34] => B [35] => C [36] => A [37] => D ) //This is the right data

I have tried array_diff_key and array_diff functions but both only returns

Array()

I would like to get the count of the difference, is there a faster way? What did I do wrong?

Upvotes: 1

Views: 1573

Answers (3)

Riz
Riz

Reputation: 10246

count(array_diff_assoc($array1, $array2));

Upvotes: 4

GG.
GG.

Reputation: 21844

You can use array_diff_assoc().

<?php

$array1 = array('34' => 'A', '35' => 'B', '36' => 'B', '37' => 'C');
$array2 = array('34' => 'B', '35' => 'C', '36' => 'A', '37' => 'D');
$count = count(array_diff_assoc($array1, $array2));

echo $count; //4

?>

Demo: http://codepad.org/Zzilrn9C

Upvotes: 0

BasicGem
BasicGem

Reputation: 83

$array1 = array ( [34] => A [35] => B [36] => B [37] => C ) 
$array2 = array( [34] => B [35] => C [36] => A [37] => D ) 
$c = count(array_diff($array1, $array2));
echo $c;

This should work for you.

Upvotes: 0

Related Questions