homepage Welcome to WebmasterWorld Guest from 54.166.113.249
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

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

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



 
Msg#: 3982152 posted 6:38 pm on Sep 1, 2009 (gmt 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.

 

httpwebwitch

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



 
Msg#: 3982152 posted 6:53 pm on Sep 1, 2009 (gmt 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.

eelixduppy

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



 
Msg#: 3982152 posted 7:06 pm on Sep 1, 2009 (gmt 0)

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

And array_diff should work, too.

httpwebwitch

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



 
Msg#: 3982152 posted 7:41 pm on Sep 1, 2009 (gmt 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.

eelixduppy

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



 
Msg#: 3982152 posted 7:48 pm on Sep 1, 2009 (gmt 0)

>> instead of altering the original arrays.

You can also change the original arrays....


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

httpwebwitch

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



 
Msg#: 3982152 posted 8:02 pm on Sep 1, 2009 (gmt 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

httpwebwitch

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



 
Msg#: 3982152 posted 8:04 pm on Sep 1, 2009 (gmt 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

eelixduppy

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



 
Msg#: 3982152 posted 8:29 pm on Sep 1, 2009 (gmt 0)

Ahh good point I wasn't thinking

mooger35

5+ Year Member



 
Msg#: 3982152 posted 10:40 pm on Sep 1, 2009 (gmt 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;
}

httpwebwitch

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



 
Msg#: 3982152 posted 11:22 pm on Sep 1, 2009 (gmt 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

pinterface

5+ Year Member



 
Msg#: 3982152 posted 12:51 am on Sep 2, 2009 (gmt 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));

;)

httpwebwitch

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



 
Msg#: 3982152 posted 1:15 pm on Sep 2, 2009 (gmt 0)

good one pinterface!
Does it work?

pinterface

5+ Year Member



 
Msg#: 3982152 posted 7:41 pm on Sep 3, 2009 (gmt 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved