PHP array_diff() Function
Simply put, array_diff() compares two arrays. The first parameter is the array the you will compare from. This means that you will find what is different in it than the second array parameter.
If it does not have any unique values that differ from array #2, the result is that there are no differences; regardless of how many items that the second array has that the first array does not have.
That was a mouthful, I hope it makes sense to you.
The easiest way to show this is to provide some examples which are shown below. Both examples consist of two arrays called $first and $second. Then, a $diff variable becomes an array that holds the difference.
Finally, the print_r() function will output the values that are different. For simplicity of explanation, there is only one value in the second array that does not exist in the first array. This makes it easy for you to understand the example.
The example below shows array_diff() with the parameters swapped. As you can see, the first print_r() statement is empty but the second print_r() statement has the value 'grape'.
<?php $first = array('apple','orange','banana'); $second = array('apple', 'orange','banana','grape'); $diff = array_diff($first,$second); print_r($diff); echo "<br/>"; $diff = array_diff($second,$first); print_r($diff);
This example changes the keys up. As you can see, the array_diff() function only compares values. The keys can be indexed numerically or associative like 'second' =>.
The code below shows the same arrays with different keys. As you can see, the results are the same.
<?php $first = array(1 => 'apple', 2 => 'orange','banana'); $second = array('first' => 'apple', 'second' => 'orange','banana','grape'); $diff = array_diff($first,$second); print_r($diff); echo "<br/>"; $diff = array_diff($second,$first); print_r($diff);
See http://lampload.com/array-diff-php.html for details on how you could use this with data from mySQL queries.