Its prototype follows:
array array_intersect(array array1, array array2 [, arrayN...])
The following example will return all states found in the $array1 that also appear
in $array2 and $array3:
158 CHAPTER 5 ?– ARRAYS
$array1 = array("OH","CA","NY","HI","CT");
$array2 = array("OH","CA","HI","NY","IA");
$array3 = array("TX","MD","NE","OH","HI");
$intersection = array_intersect($array1, $array2, $array3);
print_r($intersection);
This returns the following:
Array ( [0] => OH [3] => HI )
Note that array_intersect() considers two items to be equal only if they also
share the same datatype.
Calculating Associative Array Intersections
The function array_intersect_assoc() operates identically to array_intersect(),
except that it also considers array keys in the comparison. Therefore, only key/value
pairs located in the first array that are also found in all other input arrays will be returned
in the resulting array. Its prototype follows:
array array_intersect_assoc(array array1, array array2 [, arrayN...])
The following example returns an array consisting of all key/value pairs found in
$array1 that also appear in $array2 and $array3:
$array1 = array("OH" => "Ohio", "CA" => "California", "HI" => "Hawaii");
$array2 = array("50" => "Hawaii", "CA" => "California", "OH" => "Ohio");
$array3 = array("TX" => "Texas", "MD" => "Maryland", "OH" => "Ohio");
$intersection = array_intersect_assoc($array1, $array2, $array3);
print_r($intersection);
This returns the following:
Array ( [OH] => Ohio )
Note that Hawaii was not returned because the corresponding key in $array2 is 50
rather than HI (as is the case in the other two arrays).
Pages:
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238