This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.
Astra_Update_Header_Builder::array_merge_recursive_distinct( array $array1, array $array2, int $depth )
Recursively merge two arrays, with values from the second array overwriting the first.
Description
Unlike array_merge_recursive, this doesn’t create nested arrays for duplicate keys. Special handling: Component arrays at zone level are intelligently merged or replaced.
Parameters
- $array1
-
(array) (Required) The base array.
- $array2
-
(array) (Required) The array to merge in (takes precedence).
- $depth
-
(int) (Required) Current recursion depth.
Return
(array) The merged array.
Source
File: inc/abilities/customizer/header/class-astra-update-header-builder.php
private function array_merge_recursive_distinct( $array1, $array2, $depth = 0 ) {
$merged = $array1;
foreach ( $array2 as $key => $value ) {
if ( is_array( $value ) && isset( $merged[ $key ] ) && is_array( $merged[ $key ] ) ) {
$is_component_array = $this->is_numeric_array( $value );
if ( ! $is_component_array ) {
$merged[ $key ] = $this->array_merge_recursive_distinct( $merged[ $key ], $value, $depth + 1 );
} else {
if ( 1 === $depth ) {
$existing = $merged[ $key ];
$new = $value;
if ( empty( $new ) ) {
$merged[ $key ] = array();
} elseif ( count( $new ) === 1 && ! empty( $existing ) ) {
$new_component = $new[0];
$is_new = ! in_array( $new_component, $existing, true );
if ( $is_new ) {
$merged[ $key ] = array_values( array_unique( array_merge( $existing, $new ) ) );
} else {
$merged[ $key ] = array_values( array_unique( $value ) );
}
} else {
$merged[ $key ] = array_values( array_unique( $value ) );
}
} else {
$merged[ $key ] = $value;
}
}
} else {
$merged[ $key ] = $value;
}
}
return $merged;
}
Expand full source code Collapse full source code View on Trac