Automattic\WooCommerce\Utilities
ArrayUtil::ensure_key_is_array
Ensure that an associative array has a given key, and if not, set the key to an empty array.
Метод класса: ArrayUtil{}
Хуков нет.
Возвращает
true|false. True if the key has been added to the array, false if not (the key already existed).
Использование
$result = ArrayUtil::ensure_key_is_array( $items, $key, $throw_if_existing_is_not_array ): bool;
- $items(массив) (обязательный)
- The array to check.
- $key(строка) (обязательный)
- The key to check.
- $throw_if_existing_is_not_array(true|false)
- If true, an exception will be thrown if the key already exists in the array but the value is not an array.
По умолчанию:false
Код ArrayUtil::ensure_key_is_array() ArrayUtil::ensure key is array WC 11.0.1
public static function ensure_key_is_array( array &$items, string $key, bool $throw_if_existing_is_not_array = false ): bool {
if ( ! isset( $items[ $key ] ) ) {
$items[ $key ] = array();
return true;
}
if ( $throw_if_existing_is_not_array && ! is_array( $items[ $key ] ) ) {
$type = is_object( $items[ $key ] ) ? get_class( $items[ $key ] ) : gettype( $items[ $key ] );
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
throw new \Exception( "Array key exists but it's not an array, it's a {$type}" );
}
return false;
}