Automattic\WooCommerce\Blocks\BlockTypes\ProductCollection

QueryBuilder::array_merge_recursive_replace_non_array_properties()privateWC 1.0

Merge two array recursively but replace the non-array values instead of merging them. The merging strategy:

  • If keys from merge array doesn't exist in the base array, create them.
  • For array items with numeric keys, we merge them as normal.
  • For array items with string keys:

  • If the value isn't array, we'll use the value coming from the merge array.

    $base       = ['orderby' => 'date']
    $new_array  = ['orderby' => 'meta_value_num']
    Result: ['orderby' => 'meta_value_num']
  • If the value is array, we'll use recursion to merge each key.
    $base       = ['meta_query' => [
    [
    'key'     => '_stock_status',
    'compare' => 'IN'
    'value'   =>  ['instock', 'onbackorder']
    ]
    ]]
    $new_array  = ['meta_query' => [
    [
    'relation' => 'AND',
    [...<max_price_query>],
    [...<min_price_query>],
    ]
    ]]
    Result: ['meta_query' => [
    [
    'key'     => '_stock_status',
    'compare' => 'IN'
    'value'   =>  ['instock', 'onbackorder']
    ],
    [
    'relation' => 'AND',
    [...<max_price_query>],
    [...<min_price_query>],
    ]
    ]]
$base       = ['post__in' => [1, 2, 3, 4, 5]]
$new_array  = ['post__in' => [3, 4, 5, 6, 7]]
Result: ['post__in' => [1, 2, 3, 4, 5, 3, 4, 5, 6, 7]]

Method of the class: QueryBuilder{}

No Hooks.

Return

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->array_merge_recursive_replace_non_array_properties( $base, $new_array );
$base(array) (required)
First array.
$new_array(array) (required)
Second array.

QueryBuilder::array_merge_recursive_replace_non_array_properties() code WC 9.6.1

private function array_merge_recursive_replace_non_array_properties( $base, $new_array ) {
	foreach ( $new_array as $key => $value ) {
		if ( is_numeric( $key ) ) {
			$base[] = $value;
		} elseif ( is_array( $value ) ) {
			if ( ! isset( $base[ $key ] ) ) {
				$base[ $key ] = array();
			}
			$base[ $key ] = $this->array_merge_recursive_replace_non_array_properties( $base[ $key ], $value );
		} else {
			$base[ $key ] = $value;
		}
	}

	return $base;
}