note 78573 deleted from function.array-diff by danbrown
| From: | danbrown@php.net | Date: | Fri, 26 Jun 2009 13:57:55 +0000 |
| Subject: | note 78573 deleted from function.array-diff by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-157538@lists.php.net to get a copy of this message | ||
Note Submitter: Hayley Watson
----
A wee idiom for KEEPING only those elements of an array $a that appear in an array $b (without
introducing any elements of $b that do not appear in $a).
<?php
array_diff($a, array_diff($a, $b))
?>
And for mr dot kschan at gmail dot com: if it's the SYMMETRIC difference you want, (A-B
typically is not equal to B-A; consider the case when A and B are integers), there is Viking
Coder's comment 04-Jan-2006,
or there is the alternative:
<?php
function symmetric_array_diff($array1, $array2)
{
return array_diff(array_merge($array1, $array2), array_intersect($array1, $array2));
}
?>
Viking Coder's expression and mine are equivalent: (A-B)u(B-A) == (AuB)-(AnB). They're
probably about equal in efficiency.