comparing two arrays
| From: | Shena Delian O'Brien | Date: | Wed, 02 Jul 2003 18:15:50 +0000 |
| Subject: | comparing two arrays | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-153750@lists.php.net to get a copy of this message | ||
Hi -
I have two arrays that need to be compared. I need to know if their values match or not.
Currently I have:
$array1 = array(a, b, c, d);
$array2 = array(c, d, e, f, g);
$intersect = array_intersect($array1,$array2);
$num1 = count($array1);
$num2 = count($intersect);
Then I can compare $num1 to $num2 to see where I stand on the comparison. Less than, greater than, or equal.
Only there's a problem with this if there are identical values in an array:
$array1 = array(e, e);
$array2 = array(c, d, e, f, g);
The intersect for this returns e twice instead of the once that I need for my code. It's true that in array1 there was indeed two matches, but in array2 there was only 1 match, and that is the match that matters in my code. Does this make sense?
Is there a way to do this that I'm missing...?