John Davidson

php - Functions like array_merge with recursive processing

0 comments
Message:


I have two such arrays.


$array_1 = array(
'array_key' => array(
'array_key_1' => 1,
'array_key_2' => 2,
'array_key_3' => 3,
),
);

$array_2 = array(
'array_key' => array(
'array_key_1' => null,
'array_key_2' => null,
'array_key_3' => null,
'array_key_4' => null,
'array_key_5' => null,
),
);

I would like to create a function to create a new array from these arrays, supplemented by an array whose keys do not exist.


$array_true = array(
'array_key' => array(
'array_key_1' => 1,
'array_key_2' => 2,
'array_key_3' => 3,
'array_key_4' => null,
'array_key_5' => null
),
);

I have created my own function for array_merge, but it does not support recursion, so if there is a better way to write it, I would appreciate your advice.



$array_1 = array(
'array_key' => array(
'array_key_1' => 1,
'array_key_2' => 2,
'array_key_3' => 3,
),
);

$array_2 = array(
'array_key' => array(
'array_key_1' => null,
'array_key_2' => null,
'array_key_3' => null,
'array_key_4' => null,
'array_key_5' => null,
),
);

// Ultimately, I would like to create such a function.
$array_true = array(
'array_key' => array(
'array_key_1' => 1,
'array_key_2' => 2,
'array_key_3' => 3,
'array_key_4' => null,
'array_key_5' => null
),
);

function my_merge_func($array_1,$array_2) {
$merged = array();
foreach ($array_2 as $key => $value) {
if (empty($array_1[ $key ])) {
$merged[ $key ] = $value;
} else {
if (is_array($value)) {
// my_merge_func($array_1,$value);
// I want to make it a recursive process since similar processing continues from here.
foreach ($value as $key_1 => $value_1) {
if (empty($array_1[ $key ][ $key_1 ])) {
$merged[ $key ][ $key_1 ] = $value_1;
} else {
if (is_array($value_1)) {
foreach ($value_1 as $key_2 => $value_2) {
$merged[ $key ][ $key_1 ][ $key_2 ] = $value_2;
}
} else {
$merged[ $key ][ $key_1 ] = $array_1[ $key ][ $key_1 ];
}
}
}
} else {
$merged[ $key ] = $array_1[ $key ];
}
}
}
return $merged;
};
var_dump(my_merge_func($array_1,$array_2));

var_dump($array_true);

var_dump(my_merge_func($array_1,$array_2) === $array_true);


My Answer:
> 'value',
> 'nested_array' => array(
> 'nested_key' => 'nested_value'
> )
);

$array_2 = array(
'array_key' => 'new_value',
'nested_array' => array(
'nested_key' => 'new_nested_value'
)
);

To merge these two arrays recursively, you can use the `array_merge_recursive` function in PHP. Here's how you can do it:

php
$merged_array = array_merge_recursive($array_1, $array_2);
print_r($merged_array);


This will output:


Array
(
[array_key] => Array
(
[0] => value
[1] => new_value
)

[nested_array] => Array
(
[nested_key] => Array
(
[0] => nested_value
[1] => new_nested_value
)

)

)


As you can see, the `array_merge_recursive` function merges the two arrays while preserving the nested structure and combining the values of the same keys.

Rate this post

3 of 5 based on 9528 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us