Comparing Arrays

You have a pair of arrays, and you want to find their union (all the elements), intersection (elements in both, not just one), or difference (in one but not both).

To compute the union use array_merge() to merge the two arrays and use array_unique() to remove the duplicates.

To compute the intersection use array_intersect(). Use array_intersect_assoc()for associative arrays.

To find the simple difference use array_diff(). Use array_diff_assoc() for associative arrays.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN">
<head>
<title>PHP</title>
<meta Name="Author" Content="Hann So">
</head>
<body>
<p>
<?php
echo "<p><b>Display fruits with numerical keys:</b></p>";
$fruits = array('Apples', 'Grapes', 'Bananas', 'Oranges');
print_r($fruits);
$fruits2 = array('Apples', 'Grapes', 'Pears');
echo "<p><b>Display fruits2 with numerical keys:</b></p>";
print_r($fruits2);
$union = array_unique(array_merge($fruits, $fruits2));
echo "<p><b>Compute the union.</b></p>";
print_r($union);

$intersection = array_intersect($fruits, $fruits2);
echo "<p><b>Compute the intersection.</b></p>";
print_r($intersection);

$difference = array_diff($fruits, $fruits2);
echo "<p><b>Compute the difference between fruits and fruits2.</b></p>";
print_r($difference);

$difference = array_diff($fruits2, $fruits);
echo "<p><b>Compute the difference between fruits2 and fruits.</b></p>";
print_r($difference);

$symmetric_difference = array_merge(array_diff($fruits, $fruits2), array_diff($fruits2, $fruits));
echo "<p><b>Compute the symmetric difference.</b></p>";
print_r($symmetric_difference);

echo "<p><b>Display fruits with string keys:</b></p>";
$fruits = array('red'=>'Apples', 'green'=>'Grapes', 'yellow'=>'Bananas', 'orange'=>'Oranges');
print_r($fruits);
$fruits2 = array('red'=>'Apples', 'green'=>'Grapes', 'brown'=>'Pears');
echo "<p><b>Display fruits2 with string keys:</b></p>";
print_r($fruits2);
$union = array_unique(array_merge($fruits, $fruits2));
echo "<p><b>Compute the union.</b></p>";
print_r($union);

$intersection = array_intersect_assoc($fruits, $fruits2);
echo "<p><b>Compute the intersection.</b></p>";
print_r($intersection);

$difference = array_diff_assoc($fruits, $fruits2);
echo "<p><b>Compute the difference between fruits and fruits2.</b></p>";
print_r($difference);

$difference = array_diff_assoc($fruits2, $fruits);
echo "<p><b>Compute the difference between fruits2 and fruits.</b></p>";
print_r($difference);

$symmetric_difference = array_merge(array_diff_assoc($fruits, $fruits2), array_diff_assoc($fruits2, $fruits));
echo "<p><b>Compute the symmetric difference.</b></p>";
print_r($symmetric_difference);
?>
</p>
</body>
</html>

View the effect


Arrays (cintinued) | Adding all Values in an Array | Flipping an Array | Extracting Variables from Arrays | Imploding Arrays | Exploding Arrays | Navigating through Arrays | Merging Arrays | Comparing Arrays | Sorting Arrays | Creating Multidimensional Arrays | Sorting Multiple Arrays
© 2008: Hann So
email: hso@voyager.deanza.edu