note 98685 deleted from function.array-intersect by cmb
| From: | cmb@php.net | Date: | Sun, 09 Feb 2020 08:27:21 +0000 |
| Subject: | note 98685 deleted from function.array-intersect by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-214064@lists.php.net to get a copy of this message | ||
Note Submitter: faharanik
----
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;
}
?>