note 104063 modified in function.array-intersect by danbrown
| From: | danbrown@php.net | Date: | Fri, 20 May 2011 14:28:40 +0000 |
| Subject: | note 104063 modified in function.array-intersect by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-179908@lists.php.net to get a copy of this message | ||
The built-in function returns wrong result when input arrays have duplicate values.
Here is a code that works correctly:
<?php
function array_intersect_fixed($array1, $array2) {
$result = array();
foreach ($array1 as $val) {
if (($key = array_search($val, $array2, TRUE))!==false) {
$result[] = $val;
unset($array2[$key]);
}
}
return $result;
}
?>
--was--
The built-in function returns wrong result when input arrays have duplicate values.
Here is a code that works correctly:
function array_intersect_fixed($array1, $array2) {
$result = array();
foreach ($array1 as $val) {
if (($key = array_search($val, $array2, TRUE))!==false) {
$result[] = $val;
unset($array2[$key]);
}
}
return $result;
}
http://php.net/manual/en/function.array-intersect.php