note 20599 modified in function.array-diff by vrana
| From: | vrana@php.net | Date: | Tue, 17 Aug 2004 13:57:41 +0000 |
| Subject: | note 20599 modified in function.array-diff by vrana | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-80012@lists.php.net to get a copy of this message | ||
In PHP 4.0.6 the array_minus_array() as shown above didn't work properly, especially when you
use duplicated values:
When I wanted to substract
'Array(1,1,2,3)'
with
'Array (1,2,4)'
I wanted as result:
'Array(1,3)'
And not just 'Array (3)' as it would give using array_diff(), or nothing as result using
the above array_minus_array function.
This worked for me:
<?php
function array_minus_array($a,$b) {
$c = Array();
foreach ($a as $key => $val) {
$posb = array_search($val,$b);
if (is_integer($posb)) {
unset($b[$posb]);
} else {
$c[] = $val;
}
}
return $c;
}
?>
--was--
In PHP 4.0.6 the array_minus_array() as shown above didn't work properly, especially when you
use duplicated values:
When I wanted to substract
'Array(1,1,2,3)'
with
'Array (1,2,4)'
I wanted as result:
'Array(1,3)'
And not just 'Array (3)' as it would give using array_diff(), or nothing as result using
the above array_minus_array function.
This worked for me:
function array_minus_array($a,$b) {
$c = Array();
foreach ($a as $key => $val) {
$posb = array_search($val,$b);
if (is_integer($posb)) {
unset($b[$posb]);
} else {
$c[] = $val;
}
}
return $c;
}
http://php.net/manual/en/function.array-diff.php