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

Visit PubCon.com
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




msg:3982154
 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




msg:3982165
 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




msg:3982176
 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




msg:3982208
 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




msg:3982216
 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




msg:3982224
 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




msg:3982226
 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




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

Ahh good point I wasn't thinking

mooger35




msg:3982328
 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




msg:3982352
 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




msg:3982397
 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




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

good one pinterface!
Does it work?

pinterface




msg:3983551
 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