tags:

views:

134

answers:

2

Hi, I'm trying to merge two arrays for the values of array1 that are set

Array1 ( [0] => false [1] => 200 )
Array2 ( [0] => true [1] => 80 [2] => 60 [3] => 75 [4] => 50 [5] => 0 [6] => 0 [7] => 30 [8] => 40 )

for instance result array:

Array3 ( [0] => false [1] => 200 [2] => 60 [3] => 75 [4] => 50 [5] => 0 [6] => 0 [7] => 30 [8] => 40 )

what would be an efficient way to handle this.

Thanks

A: 

See array_merge

For array1 to override array2, use it like this:

$array3 = array_merge($array2, $array1);
David Caunt
That won't work, with numeric keys it appends to the end, doesn't overwrite.
Chad Birch
I guess I've never used it with numeric keys then - seems counterintuitive
David Caunt
Welcome to PHP, where functions take arguments in random order, and often do totally random things!
Chad Birch
Did I read the question wrong? It looks like he wants the array to be the second array appended on the end of the first... exactly what array_merge does.
seanmonstar
seanmonstar: yes, you read it wrong. He wants to merge the arrays, not append one to the other.
OIS
+3  A: 

Use the array union operator.

$array3 = $array1 + $array2;
OIS
thanks that's what I was looking for