Jump to content


Photo

arrays: if duplicate, remove value


  • Please log in to reply
7 replies to this topic

#1 levivel

levivel
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 03 September 2006 - 06:51 AM

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

#2 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 03 September 2006 - 08:05 AM

$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);


#3 levivel

levivel
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 03 September 2006 - 08:11 AM

works beautifullly, thank you kindly

L

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 03 September 2006 - 08:27 AM

or you could use the array_unique() function

<?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>';
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 03 September 2006 - 08:33 AM

Barand, that removes duplicates, but not the entire values that are duplicated.

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 03 September 2006 - 08:50 AM

Could you explain that statement
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 03 September 2006 - 08:58 AM

array_unqiue remove duplicates but he/she only wants values that does have not a duplicate when merged.


#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 03 September 2006 - 09:20 AM

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

foreach ($data2 as $val) {
    if ($k = array_search($val,$data1)) {
        unset ($data1[$k]);
    }
    else $data1[] = $val;
}

echo '<pre>', print_r($data1, true), '</pre>';

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users