Automattic\WooCommerce\Utilities
ArrayUtil::deep_compute_or_compare_array_diff
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() ArrayUtil::deep compute or compare array diff WC 11.0.1
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;
}