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_Footer_Builder::array_merge_recursive_distinct( array $array1, array $array2, int $depth )

Recursively merge two arrays, with SMART MERGING for component arrays.


Description

Unlike array_merge_recursive, this doesn’t create nested arrays for duplicate keys. Component arrays at column level use intelligent merge that auto-detects operation type.

Structure: {section: {column: [components]}}

  • Depth 0: sections (above, primary, below)
  • Depth 1: columns (primary_1, primary_2, etc) with component arrays

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/footer/class-astra-update-footer-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 ) {
						$current = $merged[ $key ];
						$new     = $value;

						// Case 1: Empty array = CLEAR column.
						if ( empty( $new ) ) {
							$merged[ $key ] = array();
						} elseif ( ! empty( array_diff( $new, $current ) ) ) {
							// Case 2: New has items not in current = ADD operation (append).
							$merged[ $key ] = array_values( array_unique( array_merge( $current, $new ) ) );
						} elseif ( ! empty( $current ) && empty( array_diff( $new, $current ) ) && count( $new ) < count( $current ) ) {
							$merged[ $key ] = array_values( array_unique( $new ) );
						} else {
							$merged[ $key ] = array_values( array_unique( $new ) );
						}
					} else {
						$merged[ $key ] = $value;
					}
				}
			} else {
				$merged[ $key ] = $value;
			}
		}

		return $merged;
	}


User Contributed Notes

You must log in before being able to contribute a note or feedback.