After I’ve asked for a method to combine 2 arrays I got several answers as you can find here:
Array combine into associative array
I’ve implemented the accepted answer but, as the author warned, I’ve run into a “duplicate keys” problem.
This is the code I’m using:
$a = array(4=>3,5=>3);
$b = array(array(0=>0,1=>4,2=>10,3=>1000),
array(0=>0,1=>40,2=>100,3=>2000),
array(4=>10)
);
$c = array();
foreach($b as $tmp) {
$c[] = $a+$tmp;
}
var_dump($c);
But it returns an array like this one:
Array (
[0] => Array (
[3] => 1
[5] => 1
[6] => 0
[7] => 0
[8] => 0
)
[1] => Array (
[3] => 1
[5] => 1
[6] => 0
[7] => 0
[8] => 0
)
[2] => Array (
[3] => 1
[5] => 1
[6] => 1
[7] => 0
[8] => 0
)
[3] => Array (
[3] => 1
[5] => 1
[6] => 1
[7] => 0
[8] => 0
)
[4] =>...
As you can see the key 0 is the same as key 1.. etc.
How can I solve this?
Thanks in advance.
Sorry, my original answer displayed a complete lack of reading comprehension skills. If I correctly understand what you’re trying to accomplish this should do the trick:
This builds up a unique array by iterating over the original array and adding new elements as it encounters them.