Concatenate two associative arrays by pushing their values ββinto a new array
I have the following two arrays:
$arrFoo = array(
'a' => 12,
'b' => 17,
);
$arrBar = array(
'a' => 9,
'c' => 4,
);
And I want the resulting array to look like this:
$arrResult = array(
'a' => array ( 12, 9 ),
'b' => array ( 17 ),
'c' => array ( 4 ),
);
Is there a sane way to achieve this without using foreach?
+3
source to share
2 answers
<?php
$arrFoo = array(
'a' => 12,
'b' => 17,
);
$arrBar = array(
'a' => 9,
'c' => 4,
);
$arrResult = array_merge_recursive($arrFoo, $arrBar);
var_dump($arr);
?>
With array_merge_recursive, you can concatenate the arrays the way you ask. http://php.net/manual/en/function.array-merge-recursive.php
+2
source to share
you can use array_merge_recursive () method
$arrResult = array_merge_recursive($arrFoo, $arrBar);
print_r($arrResult);
The result will be:
Array
(
[a] => Array
(
[0] => 12
[1] => 9
)
[b] => 17
[c] => 4
)
+2
source to share