note 43584 deleted from function.array-intersect by nlopess
| From: | nlopess@php.net | Date: | Sat, 26 Jun 2004 16:42:19 +0000 |
| Subject: | note 43584 deleted from function.array-intersect by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-71789@lists.php.net to get a copy of this message | ||
Note Submitter: workmin@ccs.neu.edu
----
I think monte at ispi dot net is confused about union and intersection. The union of two arrays is
the set which consists of all the elements from both arrays, the intersection is the set which
consists of elements that only exist in both arrays at the same time. The code he gave simply does
intersection again. Also
<?php
$arr1 = array(1,2,3,4);
$arr2 = array(2,4,6,8);
$union = array_intersect($arr2, array_intersect($arr1, $arr2));
?>
Any number of intersections with an already intersected array will always return the same array
back. Thus:
<?php
array_intersect($arr1,$arr2) == array_intersect($arr2,array_intersect($arr1,$arr2));
?>
To get the union, this code should work:
<?php
$arr1 = array(1,2,3,4);
$arr2 = array(1,4,9,10);
$inter = array_intersection($arr1,$arr2);
$union = array_diff($arr1,$inter) + $inter + $array_diff($arr2,$inter);
?>