Automattic\WooCommerce\Utilities
ArrayUtil::get_nested_value
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( $items, $key, $default_value );
- $items(массив) (обязательный)
- The array to get the value from.
- $key(строка) (обязательный)
- The complete key hierarchy, using
'::'as separator. - $default_value(разное)
- The value to return if the key doesn't exist in the array.
По умолчанию:null
Код ArrayUtil::get_nested_value() ArrayUtil::get nested value WC 11.0.0
public static function get_nested_value( array $items, string $key, $default_value = null ) {
$key_stack = explode( '::', $key );
$subkey = array_shift( $key_stack );
if ( isset( $items[ $subkey ] ) ) {
$value = $items[ $subkey ];
if ( count( $key_stack ) ) {
foreach ( $key_stack as $subkey ) {
if ( is_array( $value ) && isset( $value[ $subkey ] ) ) {
$value = $value[ $subkey ];
} else {
$value = $default_value;
break;
}
}
}
} else {
$value = $default_value;
}
return $value;
}