NYCPHP Meetup

NYPHP.org

[nycphp-talk] array_diff that works both ways ?

David Mintz dmintz at panix.com
Sat Aug 2 13:04:27 EDT 2003


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



More information about the talk mailing list