Automattic\WooCommerce\Utilities

ArrayUtil::select_as_assoc()public staticWC 1.0

Returns a new assoc array with format [ $key1 => $item1, $key2 => $item2, ... ] where $key is the value of the selector and items are original items passed.

Method of the class: ArrayUtil{}

No Hooks.

Return

Array. The converted assoc array.

Usage

$result = ArrayUtil::select_as_assoc( $items, $selector_name, $selector_type ): array;
$items(array) (required)
Items to use for conversion.
$selector_name(string) (required)
Key, method or property name to use as a selector.
$selector_type(int)
Selector type, one of the SELECT_BY_ constants.
Default: self::SELECT_BY_AUTO*

ArrayUtil::select_as_assoc() code WC 9.4.2

public static function select_as_assoc( array $items, string $selector_name, int $selector_type = self::SELECT_BY_AUTO ): array {
	$selector_callback = self::get_selector_callback( $selector_name, $selector_type );
	$result            = array();
	foreach ( $items as $item ) {
		$key = $selector_callback( $item );
		self::ensure_key_is_array( $result, $key );
		$result[ $key ][] = $item;
	}
	return $result;
}