RE: [PHP] Array differences
| From: | tedd | Date: | Wed, 14 Apr 2010 17:20:26 +0000 |
| Subject: | RE: [PHP] Array differences | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-304097@lists.php.net to get a copy of this message | ||
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. AshAsh: 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