Welcome to WebmasterWorld Guest from 54.196.110.18

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

It's not diff, it's not intersect, it's not unique, it's. ?

     
6:38 pm on Sep 1, 2009 (gmt 0)

Senior Member from CA 

WebmasterWorld Senior Member httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 29, 2003
posts:4061
votes: 0


I have two arrays

[1,2,3,4,5,6,7,8]
[2,4,6,8,10,12]

I want to remove all the elements that appear in both arrays. the result:

[1,3,5,7]
[10,12]

is there a magic potion for this? maybe something that doesn't require a lot of looping?

in PHP of course.

6:53 pm on Sept 1, 2009 (gmt 0)

Senior Member from CA 

WebmasterWorld Senior Member httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 29, 2003
posts:4061
votes: 0


maybe it can be done with array_diff

$a = [1,2,3,4,5,6,7,8]
$b = [2,4,6,8,10,12]

$c = array_diff($a,$b);
$d = array_diff($b,$a);

Sounds right to me. haven't tried it yet.

7:06 pm on Sept 1, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 12, 2005
posts:5966
votes: 0


Do they need to stay separate? Or can they be merged as one array?

And array_diff should work, too.

7:41 pm on Sept 1, 2009 (gmt 0)

Senior Member from CA 

WebmasterWorld Senior Member httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 29, 2003
posts:4061
votes: 0


yup, tried it, and it works. it does mean creating two new arrays and returning those instead of altering the original arrays.

there may be a better way, mathematically speaking. but this is... good enough.

7:48 pm on Sept 1, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 12, 2005
posts:5966
votes: 0


>> instead of altering the original arrays.

You can also change the original arrays....


$a = array_diff($a,$b);
$b = array_diff($b,$a);
8:02 pm on Sept 1, 2009 (gmt 0)

Senior Member from CA 

WebmasterWorld Senior Member httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 29, 2003
posts:4061
votes: 0


but if you do that, then $a is changed in line 1, then $b will find no diffs when it looks in $a in line 2

// input
$a = [1,2,3,4,5,6,7,8]
$b = [2,4,6,8,10,12]

// execute line 1
$a = array_diff($a,$b);

// temp values
$a = [1,3,5,7]
$b = [2,4,6,8,10,12]

// execute line 2
$b = array_diff($b,$a);

// output
$a = [1,3,5,7]
$b = [2,4,6,8,10,12]

so, at the end $b contains values that were originally present in $a, which ought to have been removed

8:04 pm on Sept 1, 2009 (gmt 0)

Senior Member from CA 

WebmasterWorld Senior Member httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 29, 2003
posts:4061
votes: 0


I suppose it could be done using one temp array, so we have a copy of $a when it's time to diff it against $b
8:29 pm on Sept 1, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 12, 2005
posts:5966
votes: 0


Ahh good point I wasn't thinking
10:40 pm on Sept 1, 2009 (gmt 0)

Full Member

10+ Year Member

joined:June 24, 2005
posts:267
votes: 0


I would actually use array_count_values [us2.php.net]

$array1 = array(1,2,3,4,5,6,7,8);
$array2 = array(2,4,6,8,10,12);

$new_array = array();
$temp = array_merge((array)$array1, (array)$array2);
$temp = array_count_values($temp);
foreach($temp as $key => $val){
if($val == 1) $new_array[] = $key;
}

11:22 pm on Sept 1, 2009 (gmt 0)

Senior Member from CA 

WebmasterWorld Senior Member httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 29, 2003
posts:4061
votes: 0


yes mooger35, your code would remove elements that are in both arrays.
but the problem states the output must be two arrays, not one merged array.

This function is essential for solving this problem [webmasterworld.com], involving summation of inherited properties in a hierarchy

12:51 am on Sept 2, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 28, 2006
posts:96
votes: 0


You can also change the original arrays...
$a = array_diff($a,$b);
$b = array_diff($b,$a);

I think you meant:

list($a, $b) = array(array_diff($a,$b), array_diff($b,$a));

;)

1:15 pm on Sept 2, 2009 (gmt 0)

Senior Member from CA 

WebmasterWorld Senior Member httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 29, 2003
posts:4061
votes: 0


good one pinterface!
Does it work?
7:41 pm on Sept 3, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 28, 2006
posts:96
votes: 0


Does it work?

... You realize, of course, it would have been faster to test it yourself than asking me if it worked. :P

But yes, that does work.

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members