RE: Array differences

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



At 9:37 AM -0600 4/14/10, Ashley M. Kirchner wrote:
No because that only does a one-way comparison.  It only tells me what's
missing from $array2.  I need it from both arrays.  That's why I'm comparing
1 versus 2, then 2 versus 1, and then doing a merge/unique on the result.

-snip-
	$array1 = array(1, 2, 3, 4, 5, 6);
	$array2 = array(1, 3, 2, 8, 9);
	$diff1  = array_diff($array1, $array2);
	$diff2  = array_diff($array2, $array1);
	$result = array_unique(array_merge($diff1, $diff2));

	=> (4, 5, 6, 8, 9)

This second $result is what I want.  So far I haven't noticed any problems
doing it this way ... yet.  I'm sure someone will tell me otherwise.

Ash

Ash:

Looks good to me. But note the indexes of the result.

You might want to:

$array1 = array(1, 2, 3, 4, 5, 6, 7, 7, 7, 7);
$array2 = array(1, 2, 3, 8, 9);
$diff1  = array_diff($array1, $array2);
$diff2  = array_diff($array2, $array1);

$diff1 = array_unique($diff1);
$diff2 = array_unique($diff2);
$result = array_merge($diff1, $diff2);

Cheers,

tedd

--
-------
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux