Tribe__Utils__Post_Collection::pluck_combine( string $key_field = '#', string|array|array $value_fields = 'post_title' )

Plucks fields from the posts in the collection creating a map using a field value as key and one or more fields as values.

Note: the method does not make any check on the uniqueness of the fields used as keys, e.g. this will probably not return what intended: $collection->pluck_combine( 'post_status', 'post_title' );. If there’s a chance of the key fields not being unique, then use # as key field to simply return an array of plucked values.


Parameters #

$key_field

(string) (Optional) The field to key the return map by, or # to use progressive integers to key the return value. Use fields as keys only when their uniqueness is sure.

Default value: '#'

$value_fields

(string|<span class="array">array|<span class="array">array) (Optional) Either a single field name to populate the values with; a list of fields, each plucked with default settings; a map of fields to fetch, each defining a single and args key to define the pluck $single and $args parameters where applicable. Additionally an as parameter can be specified to alias the field in the results. If the only requirement is to alias fields, just use a flat map like [ <orig_key_1> => <alias_1>, ... ].

Default value: 'post_title'


Top ↑

Return #

(array<int|string,string|array>) A list of plucked fields or a map of plucked fields keyed by the specified field.


Top ↑

Source #

File: src/Tribe/Utils/Post_Collection.php

	public function pluck_combine( $key_field = '#', $value_fields = 'post_title' ) {
		$value_req_is_array = is_array( $value_fields );
		$value_fields       = (array) $value_fields;
		$rows               = [];
		$field_names        = [];
		$field_index        = 0;
		foreach ( $value_fields as $k => $field ) {
			if ( is_string( $k ) && is_string( $field ) ) {
				$single     = true;
				$args       = [];
				$field_name = $field;
				$pluck      = $k;
			} else {
				list( $as, $single, $args ) = $this->parse_field_args( $field );
				$field      = is_array( $field ) ? $k : $field;
				$field_name = null === $as ? $field : $as;
				$pluck      = $field;
			}
			$field_names[ $field_index ] = $field_name;
			$rows[ $field_name ]         = $this->pluck( $pluck, $single, $args );
			$field_index ++;
		}
		$values = [];

		// Build a list with only numeric keys and string values.
		$fields_list = array_replace(
			array_filter(
				array_filter( $value_fields, 'is_string' ),
				'is_numeric',
				ARRAY_FILTER_USE_KEY
			),
			$field_names
		);

		for ( $i = 0, $count = count( $this->items ); $i < $count; $i ++ ) {
			$values[ $i ] = array_combine( $fields_list, array_column( $rows, $i ) );
		}

		if ( ! $value_req_is_array ) {
			$values = array_column( $values, reset( $fields_list ) );
		}

		// If the key field is `#` then use a progressive number as key, else use the specified field.
		$keys = '#' === $key_field
			? range( 0, count( $this->items ) - 1 )
			: $this->pluck( $key_field, true );

		return array_combine( $keys, $values );
	}

Top ↑

Changelog #

Changelog
Version Description
4.12.6 Introduced.