CHAPTER 5 ?– ARRAYS 159
Calculating Array Differences
Essentially the opposite of array_intersect(), the function array_diff() returns
those values located in the first array that are not located in any of the subseqeuent
arrays:
array array_diff(array array1, array array2 [, arrayN...])
An example follows:
$array1 = array("OH","CA","NY","HI","CT");
$array2 = array("OH","CA","HI","NY","IA");
$array3 = array("TX","MD","NE","OH","HI");
$diff = array_diff($array1, $array2, $array3);
print_r($intersection);
This returns the following:
Array ( [0] => CT )
Calculating Associative Array Differences
The function array_diff_assoc() operates identically to array_diff(), except that it
also considers array keys in the comparison. Therefore only key/value pairs located in
the first array but not appearing in any of the other input arrays will be returned in the
result array. Its prototype follows:
array array_diff_assoc(array array1, array array2 [, arrayN...])
The following example only returns "HI" => "Hawaii" because this particular key/
value appears in $array1 but doesn??™t appear in $array2 or $array3:
$array1 = array("OH" => "Ohio", "CA" => "California", "HI" => "Hawaii");
$array2 = array("50" => "Hawaii", "CA" => "California", "OH" => "Ohio");
$array3 = array("TX" => "Texas", "MD" => "Maryland", "KS" => "Kansas");
$diff = array_diff_assoc($array1, $array2, $array3);
print_r($diff);
160 CHAPTER 5 ?– ARRAYS
This returns the following:
Array ( [HI] => Hawaii )
Other Useful Array Functions
This section introduces a number of array functions that perhaps don??™t easily fall into
one of the prior sections but are nonetheless quite useful.
Pages:
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239