levivel Posted September 3, 2006 Share Posted September 3, 2006 Hello, I would like to ask for your kind assistance in this difficulty: I merged two arrays in one. In this merged array, there will be duplicate values. I would like to take those duplicate values and remove them - both, not only the duplicate value, but the original too. At the end, I would like to end up with an array with only values that did not have a duplicate after the merge. I've been searching for a solution, but this issue seems very similar to removing only the duplicate value and all my results seem to point me to the array_unique function, which does not do what I would like to. I appreciate any suggestions. Thanks, L Quote Link to comment Share on other sites More sharing options...
extrovertive Posted September 3, 2006 Share Posted September 3, 2006 [code=php:0]$data1 = array(1,2,3,4,5,6);$data2 = array(2,4,6,8);$data = array_merge($data1, $data2);$data = array_count_values($data);foreach($data as $key=>$val){ if($val == 1) $uniquedata[] = $key;}print_r($uniquedata);[/code] Quote Link to comment Share on other sites More sharing options...
levivel Posted September 3, 2006 Author Share Posted September 3, 2006 works beautifullly, thank you kindlyL Quote Link to comment Share on other sites More sharing options...
Barand Posted September 3, 2006 Share Posted September 3, 2006 or you could use the [url=http://us2.php.net/array_unique]array_unique()[/url] function[code]<?php$data1 = array(1,2,3,4,5,6);$data2 = array(2,4,6,8);$data = array_merge($data1, $data2);$uniquedata = array_unique($data);echo '<pre>', print_r($uniquedata, true), '</pre>';?>[/code] Quote Link to comment Share on other sites More sharing options...
extrovertive Posted September 3, 2006 Share Posted September 3, 2006 Barand, that removes duplicates, but not the entire values that are duplicated. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 3, 2006 Share Posted September 3, 2006 Could you explain that statement Quote Link to comment Share on other sites More sharing options...
extrovertive Posted September 3, 2006 Share Posted September 3, 2006 array_unqiue remove duplicates but he/she only wants values that does have not a duplicate when merged. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 3, 2006 Share Posted September 3, 2006 Got it, I've read the question more closely now instead of leaping in on reading the title. Still on my my first coffee of the morning.Faster method[code]foreach ($data2 as $val) { if ($k = array_search($val,$data1)) { unset ($data1[$k]); } else $data1[] = $val;}echo '<pre>', print_r($data1, true), '</pre>';[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.