Automattic\WooCommerce\Utilities

ArrayUtil::select()public staticWC 1.0

Select one single value from all the items in an array of either arrays or objects based on a selector. For arrays, the selector is a key name; for objects, the selector can be either a method name or a property name.

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

Хуков нет.

Возвращает

Массив. The selected values.

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

$result = ArrayUtil::select( $items, $selector_name, $selector_type );
$items(массив) (обязательный)
Items to apply the selection to.
$selector_name(строка) (обязательный)
Key, method or property name to use as a selector.
$selector_type(int)
Selector type, one of the SELECT_BY_ constants.
По умолчанию: self::SELECT_BY_AUTO*

Код ArrayUtil::select() WC 6.8.0

public static function select( array $items, string $selector_name, int $selector_type = self::SELECT_BY_AUTO ) {
	if ( self::SELECT_BY_OBJECT_METHOD === $selector_type ) {
		$callback = function( $item ) use ( $selector_name ) {
			return $item->$selector_name();
		};
	} elseif ( self::SELECT_BY_OBJECT_PROPERTY === $selector_type ) {
		$callback = function( $item ) use ( $selector_name ) {
			return $item->$selector_name;
		};
	} elseif ( self::SELECT_BY_ARRAY_KEY === $selector_type ) {
		$callback = function( $item ) use ( $selector_name ) {
			return $item[ $selector_name ];
		};
	} else {
		$callback = function( $item ) use ( $selector_name ) {
			if ( is_array( $item ) ) {
				return $item[ $selector_name ];
			} elseif ( method_exists( $item, $selector_name ) ) {
				return $item->$selector_name();
			} else {
				return $item->$selector_name;
			}
		};
	}

	return array_map( $callback, $items );
}