note 78573 added to function.array-diff
| From: | HayleyWatson at osu1 dot php dot net | Date: | Wed, 17 Oct 2007 20:55:35 +0000 |
| Subject: | note 78573 added to function.array-diff | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-131465@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 203.89.181.186
Probable Submitter: 60.234.168.92
----
Manual Page -- http://www.php.net/manual/en/function.array-diff.php
Edit -- https://master.php.net/note/edit/78573
Del: integrated -- https://master.php.net/note/delete/78573/integrated
Del: useless -- https://master.php.net/note/delete/78573/useless
Del: bad code -- https://master.php.net/note/delete/78573/bad+code
Del: spam -- https://master.php.net/note/delete/78573/spam
Del: non-english -- https://master.php.net/note/delete/78573/non-english
Del: in docs -- https://master.php.net/note/delete/78573/in+docs
Del: other reasons-- https://master.php.net/note/delete/78573
Reject -- https://master.php.net/note/reject/78573
Search -- https://master.php.net/manage/user-notes.php