Automattic\WooCommerce\Utilities

ArrayUtil::get_value_or_default()public staticWC 1.0

Gets the value for a given key from an array, or a default value if the key doesn't exist in the array.

This is equivalent to "$array[$key] ?? $default" except in one case: when they key exists, has a null value, and a non-null default is supplied:

$array = ['key' => null] $array['key'] ?? 'default' => 'default' ArrayUtil::get_value_or_default($array, 'key', 'default') => null

Method of the class: ArrayUtil{}

No Hooks.

Return

Mixed|null. The value for the key, or the default value passed.

Usage

$result = ArrayUtil::get_value_or_default( $items, $key, $default_value );
$items(array) (required)
The array to get the value from.
$key(string) (required)
The key to use to retrieve the value.
$default_value(null)
The default value to return if the key doesn't exist in the array.
Default: null

ArrayUtil::get_value_or_default() code WC 9.4.2

public static function get_value_or_default( array $items, string $key, $default_value = null ) {
	return array_key_exists( $key, $items ) ? $items[ $key ] : $default_value;
}