Created
May 27, 2015 14:34
-
-
Save mrbobbybryant/8a59d9bbe6f754155d4d to your computer and use it in GitHub Desktop.
Array_intersect, array_diff, and array_merge examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
echo '<p> array_intersect returns an array consisting of varables that both original array had in common.</p>'; | |
var_dump($inttoppings); | |
echo '<p> array_diff compares two arrays and returns the values from array1 that are not in array 2.</p>'; | |
var_dump($difftoppings); | |
echo '<p> array_merge simply combines the two array together into one new array.</p>'; | |
var_dump($bothtoppings); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment