I have two multidimensional arrays.
$options = [
"type" => "identity",
"document" => [
"required" => true,
"allowed_countries" => null,
"restricted_countries" => null,
"allowed_types" => null
],
"send_email" => false
// .... more options
];
$user_options = [
"type" => "identity",
"document" => [
"required" => false
],
"send_email" => false
];
I would like to merge them into something like this:
[
"type" => "identity",
"document" => [
"required" => false,
"allowed_countries" => null,
"restricted_countries" => null,
"allowed_types" => null
],
"send_email" => false
];
When I am trying to merge them, I am not getting the expected results. For example, using array_merge, I get the following.
[
"type" => "identity",
"document" => [
"required" => false,
],
"send_email" => false
];
It removes these keys:
"allowed_countries" => null,
"restricted_countries" => null,
"allowed_types" => null
array_merge_recursive returns the required keys combined in one array:
"document" => array:4 [▼
"required" => array:2 [▼
0 => true
1 => false
]
I also tried collections and the merge method in Laravel, but I didn’t get the expected results and I don’t want that. What can I do?
2
Answers
Can always go the recursive way:
You can use the
array_replace_recursive()
function in PHP.