WP_CLI

Formatter::show_single_field()privateWP-CLI 1.0

Show a single field from a list of items.

Method of the class: Formatter{}

No Hooks.

Return

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->show_single_field( $items, $field );
$items(array) (required)
Array of objects to show fields from
$field(string) (required)
The field to show

Formatter::show_single_field() code WP-CLI 2.8.0-alpha

private function show_single_field( $items, $field ) {
	$key    = null;
	$values = [];

	foreach ( $items as $item ) {
		$item = (object) $item;

		if ( null === $key ) {
			$key = $this->find_item_key( $item, $field );
		}

		if ( 'json' === $this->args['format'] ) {
			$values[] = $item->$key;
		} else {
			WP_CLI::print_value(
				$item->$key,
				[
					'format' => $this->args['format'],
				]
			);
		}
	}

	if ( 'json' === $this->args['format'] ) {
		echo json_encode( $values );
	}
}