How to compare the differences between two arrays in PHP

Posted on October 30th, 2008 in Uncategorized | Tags: , ,

Question: How do I write a function to compare the differences between two arrays?

Consider the following scenarios:

$alreadyInTheDatabase = array('name' => 'Joe', 'address' => '123 Fake St');
$newFormValuesArray = array('name' => 'Joe', 'address' => 'My address has changed');

$alreadyInTheDatabase = array('name' => 'Joe', 'address' => '123 Fake St');
$newFormValuesArray = array('name' => 'Joe'); //a bad form has address missing from the array

$alreadyInTheDatabase = array('name' => 'Joe'); //address was never in the database to begin with
$newFormValuesArray = array('name' => 'Joe', 'address' => '123 Fake St');

I’m writing a function to check whether or not I need to update a field in a database, so my function will tell me what the differences are between what’s already in the database, and what form fields have changed. If the field is the same as the one that’s in the database, I won’t need to update it. If the values are different, I’ll update. If the form has a field that doesn’t even exist yet in the database (because it hasn’t been created yet), I’ll need to know that too. So I’ll just return an array of the keys that differ.

There are a lot of PHP functions that exist to solve all sorts of scenarios where you would want to compare arrays, but none of them return an array of the differences between both arrays.

array_diff — Computes the difference of arrays
array_udiff — Computes the difference of arrays by using a callback function for data comparison
array_diff_key — Computes the difference of arrays using keys for comparison
array_diff_uassoc — Computes the difference of arrays with additional index check which is performed by a user supplied callback function
array_diff_ukey — Computes the difference of arrays using a callback function on the keys for comparison
array_intersect — Computes the intersection of arrays
array_uintersect — Computes the intersection of arrays, compares data by a callback function
array_intersect_assoc — Computes the intersection of arrays with additional index check
array_intersect_key — Computes the intersection of arrays using keys for comparison
array_intersect_uassoc — Computes the intersection of arrays with additional index check, compares indexes by a callback function
array_intersect_ukey — Computes the intersection of arrays using a callback function on the keys for comparison
array_udiff_assoc — Computes the difference of arrays with additional index check, compares data by a callback function
array_udiff_uassoc — Computes the difference of arrays with additional index check, compares data and indexes by a callback function
array_uintersect_assoc — Computes the intersection of arrays with additional index check, compares data by a callback function
array_uintersect_uassoc — Computes the intersection of arrays with additional index check, compares data and indexes by a callback functions

Quite a lengthy list, isn’t it?? Now you can sense my confusion and frustration. We want to check the values, but return the key of the value that is different. We’ll also check the keys to see if we need to create a field that doesn’t already exist in the database.

Answer: We’ll use array_diff_assoc() to return an associative array of the keys and values that differ in the first array. Then we’ll swap the arrays to check the second array against the first. We’ll merge the two arrays of differences using array_merge() and return just the keys by using array_keys().

function getDifferences($alreadyInTheDatabase, $newFormValuesArray) {

$diff1 = array_diff_assoc($alreadyInTheDatabase, $newFormValuesArray);
$diff2 = array_diff_assoc($newFormValuesArray, $alreadyInTheDatabase);

$merged = array_merge($diff1, $diff2);

$justTheKeys = array_keys($merged);

return $justTheKeys;

}

Leave a Comment

Contact Form