note 98685 added to function.array-intersect
| From: | faharanik at osu1 dot php dot net | Date: | Wed, 30 Jun 2010 14:30:12 +0000 |
| Subject: | note 98685 added to function.array-intersect | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-170603@lists.php.net to get a copy of this message | ||
Here's my approach to intersection returning only the values present in all the arrays.
Note that each array must not contain duplicate values.
I don't know how effective it actually is, but perhaps it could help.
<?php
// $arrays - Array of arrays to intersect.
function calculate_intersection($arrays)
{
$intersection = Array();
for($checked_item = 0; $checked_item < count($arrays[0]); $checked_item++)
{
$occurrence = 1;
for($compared_array = 1; $compared_array < count($arrays); $compared_array++)
{
for($compared_item = 0; $compared_item < count($arrays[$compared_array]); $compared_item++)
{
if($arrays[0][$checked_item] == $arrays[$compared_array][$compared_item])
{
$occurrence++;
if($occurrence == count($arrays))
{
$intersection[] = $arrays[0][$checked_item];
}
}
}
}
}
return $intersection;
}
?>
----
Server IP: 147.32.160.17
Probable Submitter: 213.192.26.250
----
Manual Page -- http://www.php.net/manual/en/function.array-intersect.php
Edit -- https://master.php.net/note/edit/98685
Del: integrated -- https://master.php.net/note/delete/98685/integrated
Del: useless -- https://master.php.net/note/delete/98685/useless
Del: bad code -- https://master.php.net/note/delete/98685/bad+code
Del: spam -- https://master.php.net/note/delete/98685/spam
Del: non-english -- https://master.php.net/note/delete/98685/non-english
Del: in docs -- https://master.php.net/note/delete/98685/in+docs
Del: other reasons-- https://master.php.net/note/delete/98685
Reject -- https://master.php.net/note/reject/98685
Search -- https://master.php.net/manage/user-notes.php