Automattic\WooCommerce\Utilities

ArrayUtil::ensure_key_is_array()public staticWC 1.0

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( $array, $key, $throw_if_existing_is_not_array ): bool;
$array(массив) (обязательный)
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() WC 7.1.0

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;
}