Hudzilla.org - the homepage of Paul Hudson
Contents > Arrays > Array-specific functions Wish List | Report Bug | About Me ]

5.6.1     Chopping and changing arrays: array_diff(), array_intersect(), and array_merge()

This is NOT the latest copy of this book; click here for the latest version.

array array_diff ( array array1, array array2 [, array ...])

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

array array_merge ( array array1, array array2 [, array ...])

There are three key functions that handle the comparison of two arrays to create a new array, and they work in much the same way - they take a minimum of two arrays as parameters (we will call them $arr1 and $arr2), and return an array for a result.

The difference is that array_diff() returns a new array containing all the values of $arr1 that do not exist in $arr2, array_intersect() returns a new array containing all the values of $arr1 that do exist in $arr2, and array_merge() just combines the two arrays. These three functions work well as a team, and allow you to construct combined arrays out of individual arrays.

Here is an example with these three functions in action:

<?php
    $toppings1
= array("Pepperoni", "Cheese", "Anchovies", "Tomatoes");
    
$toppings2 = array("Ham", "Cheese", "Peppers");
    
$inttoppings = array_intersect($toppings1, $toppings2);
    
$difftoppings = array_diff($toppings1, $toppings2);
    
$bothtoppings = array_merge($toppings1, $toppings2);
    
var_dump($inttoppings);
    
var_dump($difftoppings);
    
var_dump($bothtoppings);
?>

As you can see, there are two arrays of pizza toppings, and they both share cheese as a value. Here is what that script outputs:

array(1) { [1]=> string(6) "Cheese" }
array(3) { [0]=> string(9) "Pepperoni" [2]=> string(9) "Anchovies" [3]=> string(8) "Tomatoes" }
array(7) { [0]=> string(9) "Pepperoni" [1]=> string(6) "Cheese" [2]=> string(9) "Anchovies" [3]=> string(8) "Tomatoes" [4]=> string(3) "Ham" [5]=> string(6) "Cheese" [6]=> string(7) "Peppers" }

Array_intersect(), if you recall, returns an array listing all values in array parameter one that are also in array parameter two, so this call in our example will put just one element, cheese, into $inttoppings. The call to array_diff() will place into $difftoppings all the values from $toppings1 that aren't in $toppings2, namely pepperoni, anchovies, and tomatoes.

Finally we have a call to array_merge(), which results in seven elements being placed into $bothtoppings: pepperoni, cheese, anchovies, tomatoes, peppers, ham, and cheese. As you can see, "cheese" is in there twice, so what you really need now is a way to strip out duplicate values.

Author's Note: The + operator in PHP is overloaded so that you can use it to merge arrays, eg $array3 = $array1 + $array2, but will ignore any key clashes, and so is quite confusing to use unless you have unique keys.





<< 5.6 Array-specific functions   5.6.2 Stripping out duplicate values: array_unique() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is four plus ten?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow