NYCPHP Meetup

[nycphp-talk] array_diff that works both ways ?

Jon Baer jonbaer at jonbaer.net
Sat Aug 2 16:23:04 EDT 2003


dont think u need to sort, u could just merge results:

$array1 = array(1,2,3);
$array2 = array(2,3,4);
$array3 = array_merge(array_diff($array1, $array2), array_diff($array2,
$array1));
print_r($array3);

but i think u r right there should be a "array_diff_recursive" method or
something.

- jon

pgp key: http://www.jonbaer.net/jonbaer.asc
fingerprint: F438 A47E C45E 8B27 F68C 1F9B 41DB DB8B 9A0C AF47


----- Original Message ----- 
From: "David Mintz" <dmintz at panix.com>
To: <chendry at nyc.rr.com>; "NYPHP Talk" <talk at lists.nyphp.org>
Sent: Saturday, August 02, 2003 10:04 AM
Subject: [nycphp-talk] array_diff that works both ways ?


>
> I'm wondering what's the most efficient way to find out if $array1 and
> $array2 contain exactly the same elements, without regard to order.
> array_diff(array1,array2) "returns an array containing all the values of
> array1  that are not present in any of the other arguments." according to
> TFM. Assuming
>
> $array1 = array(1,2,3);
> $array2 = array(2,3,4);
>
> then array_diff($array1,$array2) would return an array containing 1. I'd
> like to get one containing (1,4). I know I could sort them both and then
> test them for equality. I just wonder if there's a more efficient
> technique or if I'm overlooking some native function that does this.
>
> (FWIW, it's for a home-grown shopping cart kind of thing and the objective
> is to detect whether the array of product ids they just POSTed is any
> different from its counterpart in $_SESSION, to avoid doing needless
> database calls).
>
> Always gratefully,
>
>
> ---
> David Mintz
> http://davidmintz.org/
> Email: See http://dmintzweb.com/whitelist.php first!
>
> Decibels of sound pressure employed by a new "thermoacoustic"
refrigerator: 190
> Decibels of sound pressure sufficient to ignite a person's hair: 165
>
> -- Harper's Index, February 2003
> _______________________________________________
> talk mailing list
> talk at lists.nyphp.org
> http://lists.nyphp.org/mailman/listinfo/talk
>




More information about the talk mailing list