I have 2 arrays in PHP, one which simulates properties that can be edited, the other with the properties and the values it should have.
An example is as following:
$arr1 = array(
'entity1' => array(
'property1' => null,
'property2' => null,
),
'entity2' => array(
'property3' => null,
'property4' => null,
'property5' => null,
)
);
$arr2 = array(
'entity1' => array(
'property1' => 1,
'property2' => 0,
),
'entity2' => array(
'property3' => 0,
'property4' => 1,
)
);
Now I need to merge these arrays and keep the null values, so that the output will be as following:
$output = array(
'entity1' => array(
'property1' => 1,
'property2' => 0,
),
'entity2' => array(
'property3' => 0,
'property4' => 1,
'property5' => null,
)
);
I tried array_merge()
and $arr1 + $arr2
which both output this:
Array
(
[entity1] => Array
(
[property1] => 1
[property2] => 0
)
[entity2] => Array
(
[property3] => 0
[property4] => 1
)
)
The array can be deeper than displayed in the example, so it would have to be recursive, and the arrays are all associative.
I have read numerous questions on StackOverflow and Googled quite abit, but the answers I found are not recursive or just don't apply to this question.
EDIT
As above explanation for why I asked this question even though I checked other questions with answers is apparently not enough proof or something, I tried the marked duplicated questions answer, which is the following:
$merged = array_merge(array_filter($arr1, 'strval'), array_filter($arr2, 'strval'));
it gave an array to string conversion error cause it is not recursive, which is what I explained.