Automattic\WooCommerce\Utilities

ArrayUtil::deep_compute_or_compare_array_diff()private staticWC 1.0

Helper method to compare to compute difference between two arrays. Comparison is done recursively.

{} Это метод класса: ArrayUtil{}

Хуков нет.

Возвращает

Массив|true|false. The difference between the two arrays, or if array are same, depending upon $compare param.

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

$result = ArrayUtil::deep_compute_or_compare_array_diff( $array1, $array2, $compare, $strict );
$array1(массив) (обязательный)
First array.
$array2(массив) (обязательный)
Second array.
$compare(true|false) (обязательный)
Whether to compare the arrays. If true, then function will return false on first difference, in order to be slightly more efficient.
$strict(true|false)
Whether to do string comparison.
По умолчанию: true

Код ArrayUtil::deep_compute_or_compare_array_diff() WC 7.1.0

private static function deep_compute_or_compare_array_diff( array $array1, array $array2, bool $compare, bool $strict = true ) {
	$diff = array();
	foreach ( $array1 as $key => $value ) {
		if ( is_array( $value ) ) {
			if ( ! array_key_exists( $key, $array2 ) || ! is_array( $array2[ $key ] ) ) {
				if ( $compare ) {
					return true;
				}
				$diff[ $key ] = $value;
			}
			$new_diff = self::deep_assoc_array_diff( $value, $array2[ $key ], $strict );
			if ( ! empty( $new_diff ) ) {
				if ( $compare ) {
					return true;
				}
				$diff[ $key ] = $new_diff;
			}
		} elseif ( $strict ) {
			if ( ! array_key_exists( $key, $array2 ) || $value !== $array2[ $key ] ) {
				if ( $compare ) {
					return true;
				}
				$diff[ $key ] = $value;
			}
		} else {
			// phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison -- Intentional when $strict is false.
			if ( ! array_key_exists( $key, $array2 ) || $value != $array2[ $key ] ) {
				if ( $compare ) {
					return true;
				}
				$diff[ $key ] = $value;
			}
		}
	}

	return $compare ? false : $diff;
}