Automattic\WooCommerce\Utilities

ArrayUtil::get_nested_value()public staticWC 1.0

Get a value from an nested array by specifying the entire key hierarchy with '::' as separator.

E.g. for [ 'foo' => [ 'bar' => [ 'fizz' => 'buzz' ] ] ] the value for key 'foo::bar::fizz' would be 'buzz'.

Метод класса: ArrayUtil{}

Хуков нет.

Возвращает

Разное. The retrieved value, or the supplied default value.

Использование

$result = ArrayUtil::get_nested_value( $array, $key, $default );
$array(массив) (обязательный)
The array to get the value from.
$key(строка) (обязательный)
The complete key hierarchy, using '::' as separator.
$default(разное)
The value to return if the key doesn't exist in the array.
По умолчанию: null

Код ArrayUtil::get_nested_value() WC 8.7.0

public static function get_nested_value( array $array, string $key, $default = null ) {
	$key_stack = explode( '::', $key );
	$subkey    = array_shift( $key_stack );

	if ( isset( $array[ $subkey ] ) ) {
		$value = $array[ $subkey ];

		if ( count( $key_stack ) ) {
			foreach ( $key_stack as $subkey ) {
				if ( is_array( $value ) && isset( $value[ $subkey ] ) ) {
					$value = $value[ $subkey ];
				} else {
					$value = $default;
					break;
				}
			}
		}
	} else {
		$value = $default;
	}

	return $value;
}