PHP also includes support for comparing arrays and determining which elements are the sameor which are different. For example, say you have these two arrays, where only the second element is the same: $local_fruits = array("apple", "pomegranate", "orange"); $tropical_fruits = array("pineapple", "pomegranate", "papaya"); You can use the array_diff function to create a new array, which we'll call $difference, that holds the elements that are different between the two arrays: <?php $local_fruits = array("apple", "pomegranate", "orange"); $tropical_fruits = array("pineapple", "pomegranate", "papaya"); $difference = array_diff($local_fruits, $tropical_fruits); foreach ($difference as $key => $value) { echo "Key: $key; Value: $value\n"; } ?> Here's what this script displays: Key: 0; Value: apple Key: 2; Value: orange Now say you're working with two arrays that use text indexes, and you want to see which elements have either different keys or values when comparing the arrays: $local_fruits = array("fruit1" => "apple", "fruit2" => "pomegranate", "fruit3" => "orange"); $tropical_fruits = array("fruit1" => "pineapple", "fruit_two" => "pomegranate", "fruit3" => "papaya"); You can determine which array elements have either different keys or values by using the array_diff_assoc function (arrays with text indexes are also called associative arrays, hence the name array_diff_assoc) this way: <?php $local_fruits = array("fruit1" => "apple", "fruit2" => "pomegranate", "fruit3" => "orange"); $tropical_fruits = array("fruit1" => "pineapple", "fruit_two" => "pomegranate", "fruit3" => "papaya"); $difference = array_diff_assoc($local_fruits, $tropical_fruits); foreach ($difference as $key => $value) { echo "Key: $key; Value: $value\n"; } ?> And here's what you getnote that we've been able to find all array elements that differ in either key or value: Key: fruit1; Value: apple Key: fruit2; Value: pomegranate Key: fruit3; Value: orange What if you want to find all array elements that the arrays have in common instead? In that case, use array_intersect. Here's an example, where we're finding the elements in common between our two arrays: <?php $local_fruits = array("apple", "pomegranate", "orange"); $tropical_fruits = array("pineapple", "pomegranate", "papaya"); $common = array_intersect($local_fruits, $tropical_fruits); foreach ($common as $key => $value) { echo "Key: $key; Value: $value\n"; } ?> And this is what you get: Key: 1; Value: pomegranate You can also do the same with arrays that use text indexes if you use array_intersect_assoc: <?php $local_fruits = array("fruit1" => "apple", "fruit2" => "pomegranate", "fruit3" => "orange"); $tropical_fruits = array("fruit1" => "pineapple", "fruit2" => "pomegranate", "fruit3" => "papaya"); $common = array_intersect_assoc($local_fruits, $tropical_fruits); foreach ($common as $key => $value) { echo "Key: $key; Value: $value\n"; } ?> And here's what this script gives you: Key: fruit2; Value: pomegranate Comparing arrays in PHP? No problem at all. |