Welcome to WebmasterWorld Guest from 54.146.239.96

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

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

     

httpwebwitch

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

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



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.

httpwebwitch

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

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



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.

eelixduppy

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

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



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

And array_diff should work, too.

httpwebwitch

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

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



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.

eelixduppy

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

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



>> instead of altering the original arrays.

You can also change the original arrays....


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

httpwebwitch

8:02 pm on Sep 1, 2009 (gmt 0)

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



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

httpwebwitch

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

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



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

eelixduppy

8:29 pm on Sep 1, 2009 (gmt 0)

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



Ahh good point I wasn't thinking

mooger35

10:40 pm on Sep 1, 2009 (gmt 0)

10+ Year Member



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;
}

httpwebwitch

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

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



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

pinterface

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

5+ Year Member



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

;)

httpwebwitch

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

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



good one pinterface!
Does it work?

pinterface

7:41 pm on Sep 3, 2009 (gmt 0)

5+ Year Member



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.

 

Featured Threads

Hot Threads This Week

Hot Threads This Month