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.

Method of the class: ArrayUtil{}

No Hooks.

Return

Array|true|false. The difference between the two arrays, or if array are same, depending upon $compare param.

Usage

$result = ArrayUtil::deep_compute_or_compare_array_diff( $array1, $array2, $compare, $strict );
$array1(array) (required)
First array.
$array2(array) (required)
Second array.
$compare(true|false) (required)
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.
Default: true

ArrayUtil::deep_compute_or_compare_array_diff() code WC 9.3.3

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;
				continue;
			}
			$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;
			}
            // phpcs:ignore Universal.Operators.StrictComparisons.LooseNotEqual -- Intentional when $strict is false.
		} elseif ( ! array_key_exists( $key, $array2 ) || $value != $array2[ $key ] ) {
			if ( $compare ) {
				return true;
			}
			$diff[ $key ] = $value;
		}
	}

	return $compare ? false : $diff;
}