note 43584 added to function.array-intersect

From: Date: Sat, 26 Jun 2004 15:28:22 +0000
Subject: note 43584 added to function.array-intersect
Groups: php.notes 
Request: Send a blank email to php-notes+get-71786@lists.php.net to get a copy of this message
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); ?> ---- Manual Page -- http://www.php.net/manual/en/function.array-intersect.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+43584 Delete -- http://master.php.net/manage/user-notes.php?action=delete+43584&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+43584&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#71786) next »