I am new in PHP and love to learn it. I want to merge two or more arrays having same keys only. And neglect the arrays whose keys are not in both the Arrays. Like
Here is the First Array :
Array
(
[1] => Array
(
[111] => 36265
)
[2] => Array
(
[222] => 36265
)
[3] => Array
(
[333] => 36265
)
)
and Second Array as :
Array
(
[1] => Array
(
[444] => 36265
)
[2] => Array
(
[555] => 36265
)
[4] => Array
(
[666] => 36265
)
)
And i want my result to be as :
Array
(
[1] => Array
(
[111] => 36265
[444] => 36265
)
[2] => Array
(
[222] => 36265
[555] => 36265
)
)
Neglecting the rest Array with key [3] & [4]....
So Please anyone tell me how to get this. I try out "array_merge_recursive()" but this one displays all keys. Thanks in advance.
You'd have to loop over one of the arrays, check for the existence of the current key in the other array, if it exists, merge them, eg:
$output = array();
foreach ($array1 as $key => $value) {
if (array_key_exists($key, $array2)) {
$output[$key] = $value + $array2[$key];
}
}
0 comments:
Post a Comment