Thursday, 8 August 2013

The best way to "add" arrays in this case

The best way to "add" arrays in this case

I have two arrays of the same dimension. Each value in arrays is a number
a or b.
What's the fastest way to get $array3 of the same dimension which contains:
a, if both values are a,
b, if both values are b,
0, if the values are different.
$array1 = array(a,b,b,a,a,b,a,b,a,a,b,b,a);
$array2 = array(a,b,a,b,a,a,b,a,b,b,a,b,a);
==================
$array3 = array(a,b,0,0,a,0,0,0,0,0,0,b,a);
What about performance, I believe using loop statement is not the best way.
I belive something like 60&13 = 12 could be used in a proper way but I
don't know neither the way nor how to use it for arrays nor how to use it
for big numbers (if the array is huge). Or, using anonymous functions
would be the best?
Any ideas for the best (but not least) solution? Thank you.

No comments:

Post a Comment