tic function deep_assoc_array_diff( array $array1, array $array2, bool $strict = true ): array { return self::deep_compute_or_compare_array_diff( $array1, $array2, false, $strict ); } /** * Helper method to compare to compute difference between two arrays. Comparison is done recursively. * * @param array $array1 First array. * @param array $array2 Second array. * @param bool $compare Whether to compare the arrays. If true, then function will return false on first difference, in order to be slightly more efficient. * @param bool $strict Whether to do string comparison. * * @return array|bool The difference between the two arrays, or if array are same, depending upon $compare param. */ 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; } /** * Push a value to an array, but only if the value isn't in the array already. * * @param array $array The array. * @param mixed $value The value to maybe push. * @return bool True if the value has been added to the array, false if the value was already in the array. */ public static function push_once( array &$array, $value ) : bool { if ( in_array( $value, $array, true ) ) { return false; } $array[] = $value; return true; } /** * Ensure that an associative array has a given key, and if not, set the key to an empty array. * * @param array $array The array to check. * @param string $key The key to check. * @param bool $throw_if_existing_is_not_array If true, an exception will be thrown if the key already exists in the array but the value is not an array. * @return bool True if the key has been added to the array, false if not (the key already existed). * @throws \Exception The key already exists in the array but the value is not an array. */ public static function ensure_key_is_array( array &$array, string $key, bool $throw_if_existing_is_not_array = false ): bool { if ( ! isset( $array[ $key ] ) ) { $array[ $key ] = array(); return true; } if ( $throw_if_existing_is_not_array && ! is_array( $array[ $key ] ) ) { $type = is_object( $array[ $key ] ) ? get_class( $array[ $key ] ) : gettype( $array[ $key ] ); throw new \Exception( "Array key exists but it's not an array, it's a {$type}" ); } return false; } }