note 82143 added to function.array-diff
| From: | merlinyoda at dorproject dot net | Date: | Fri, 28 Mar 2008 18:11:00 +0000 |
| Subject: | note 82143 added to function.array-diff | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-137683@lists.php.net to get a copy of this message | ||
As touched on in kitchin's comment of 19-Jun-2007 03:49 and nilsandre at gmx dot de's
comment of 17-Jul-2007 10:45, array_diff's behavior may be counter-intuitive if you aren't
thinking in terms of set theory.
array_diff() returns a *mathematical* difference (a.k.a. subtraction) of elements in array A that
are in array B and *not* what elements are different between the arrays (i.e. those that elements
that are in either A or B but aren't in both A and B).
Drawing one of those Ven diagrams or Euler diagrams may help with visualization...
As far as a function for returning what you may be expecting, here's one:
<?php
function array_xor ($array_a, $array_b) {
$union_array = array_merge($array_a, $array_b);
$intersect_array = array_intersect($array_a, $array_b);
return array_diff($union_array, $intersect_array)
}
?>
----
Server IP: 209.41.74.194
Probable Submitter: 12.47.22.186
----
Manual Page -- http://www.php.net/manual/en/function.array-diff.php
Edit -- https://master.php.net/note/edit/82143
Del: integrated -- https://master.php.net/note/delete/82143/integrated
Del: useless -- https://master.php.net/note/delete/82143/useless
Del: bad code -- https://master.php.net/note/delete/82143/bad+code
Del: spam -- https://master.php.net/note/delete/82143/spam
Del: non-english -- https://master.php.net/note/delete/82143/non-english
Del: in docs -- https://master.php.net/note/delete/82143/in+docs
Del: other reasons-- https://master.php.net/note/delete/82143
Reject -- https://master.php.net/note/reject/82143
Search -- https://master.php.net/manage/user-notes.php