Automattic\WooCommerce\Blocks\BlockTypes

ProductQuery::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 comming from the merge array.

    $base = ['orderby' => 'date']
    $new  = ['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  = ['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  = ['post__in' => [3, 4, 5, 6, 7]]
Result: ['post__in' => [1, 2, 3, 4, 5, 3, 4, 5, 6, 7]]

Метод класса: ProductQuery{}

Хуков нет.

Возвращает

null. Ничего (null).

Использование

// private - только в коде основоного (родительского) класса
$result = $this->array_merge_recursive_replace_non_array_properties( $base, $new );
$base(массив) (обязательный)
First array.
$new(массив) (обязательный)
Second array.

Код ProductQuery::array_merge_recursive_replace_non_array_properties() WC 8.7.0

private function array_merge_recursive_replace_non_array_properties( $base, $new ) {
	foreach ( $new as $key => $value ) {
		if ( is_numeric( $key ) ) {
			$base[] = $value;
		} else {
			if ( 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;
}