0

Arrays have been comprehensively covered but am still stumped about how to go around this. I have two arrays which i want to merge without overwriting duplicate keys i.e.

Array1
(
    [0] => 0
    [1] => 1
    [2] => 1
    [3] => 1
    [4] => 1
    [5] => 0
    [6] => 0
)

+ 

Array2
(
    [0] => 0
    [1] => 0
    [2] => 0
    [3] => 0
    [4] => 0
    [5] => 0
    [6] => 1
)

my ideal result is

Array1 + Array2
(
    [0] => 0
    [1] => 1
    [2] => 1
    [3] => 1
    [4] => 1
    [5] => 0
    [6] => 1
)

How would i do this? I've tried using + but it gives the first array as the result

3
  • 1
    So are you saying you want to select the higher value from each of the two array keys? Or you want to mathematically add the total of the two arrays at each key? Commented Jan 26, 2012 at 19:16
  • 1
    Your requirements are inconsistent. Do you want to use values from first array unless overwritten by the second, or you want to do something opposite? In both cases the result you are showing is incorrect. Make up your mind. Commented Jan 26, 2012 at 19:17
  • The array1 + array2 is the solution. Could you please provide an example of the code you tried? Commented Jan 26, 2012 at 19:22

7 Answers 7

6

What you want to do is to map both arrays into single array, containing max value from two respective values, like that:

$array1 = array(0, 1, 1, 1, 1, 0, 0);
$array2 = array(0, 0, 0, 0, 0, 0, 1);

$result = array_map('max', $array1, $array2);

See the result here: http://ideone.com/clone/MN568

It looks like that:

array(7) {
  [0]=>
  int(0)
  [1]=>
  int(1)
  [2]=>
  int(1)
  [3]=>
  int(1)
  [4]=>
  int(1)
  [5]=>
  int(0)
  [6]=>
  int(1)
}
Sign up to request clarification or add additional context in comments.

2 Comments

+1 for intelligent array_map usage over looping. I assume this is what the OP was trying to do, but with the vague question, who knows?
This worked perfectly, just what I wanted. Thanks for the quick response guys :-)
0

array_merge() does not overwrite duplicate elements that have numeric keys.

1 Comment

But the numeric keys that are not overwritten will be appended at the end of the array...
0

Given the arrays are of the same length:

function bitwise_or_arrays($arr1, $arr2) {
    $result = array();
    for ($i = 0; $i < count($arr1); $i++) {
        $result[$i] = $arr1 | $arr2;
    }
    return $result;
}

Comments

0

If you are looking for the greater (nonzero) of the two arrays, you can iterate like so:

$array1 = array(1,0,0,1,1,1);
$array2 = array(0,0,1,0,0,1);

$newarr = array();
foreach ($array1 as $k => $v) {
  $newarr[$k] = max($array1[$k], $array2[$k]);
}

print_r($newarr);
Array
(
    [0] => 1
    [1] => 0
    [2] => 1
    [3] => 1
    [4] => 1
    [5] => 1
)

If what you need is to add the values, use:

$newarr = array();
foreach ($array1 as $k => $v) {
  $newarr[$k] = $array1[$k] + $array2[$k];
}

Comments

0

Just for fun (although in your case with 0 and 1 values it works :)

$array1 = array(0, 1, 1, 1, 1, 0, 0);
$array2 = array(0, 0, 0, 0, 0, 0, 1);

$str1 = implode('', $array1);
$str2 = implode('', $array2);

$result = str_split($str1 | $str2);

Sorry for this variant, I know it's crazy, but just couldn't not to post it. Arrays look like bit masks 0111100 and 0000001. So just using bitwise | operator.

So result:

Array
(
    [0] => 0
    [1] => 1
    [2] => 1
    [3] => 1
    [4] => 1
    [5] => 0
    [6] => 1
)

Comments

0

If what your looking is to combine them use array_combine().

$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);

print_r($c);

//*output:
array(
[green]  => avocado
[red]    => apple
[yellow] => banana
)

here's how to merge array:

 $beginning = 'foo';
 $end = array(1 => 'bar');
 $result = array_merge((array)$beginning, (array)$end);
 print_r($result);
//output:
 Array(
    [0] => foo
    [1] => bar
)

heres how to add values

$a = array(0=>1, 1=>2, 2=>3, 3=>4);
$b = array(0=>5, 1=>6, 2=>7, 3=>8);


$c = $a[0] += $b[0]; 

print_r($c);//output: 6

im not a guru on php but i hope this helps you even just abit.

Comments

0

Just for the fun of it:

$array1 = array(0,1,1,1,1,0,0);
$array2 = array(0,0,0,0,0,0,1);

$array3 = str_split(decbin(bindec(implode('',$array1)) | bindec(implode('',$array2))));

var_dump($array3);

Unfortunately it trims leading zeroes

Using

$array3 = str_split(str_pad(decbin(bindec(implode('',$array1)) | bindec(implode('',$array2))),count($array1),'0',STR_PAD_LEFT));

will restore leading zeroes, but doesn't feel as clean

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.