WP_CLI\Utils

format_items()WP-CLI 1.0

Render a collection of items as an ASCII table, JSON, CSV, YAML, list of ids, or count.

Given a collection of items with a consistent data structure:

$items = array(
	array(
		'key'   => 'foo',
		'value'  => 'bar',
	)
);

Render $items as an ASCII table:

WP_CLI\Utils\format_items( 'table', $items, array( 'key', 'value' ) );

# +-----+-------+
# | key | value |
# +-----+-------+
# | foo | bar   |
# +-----+-------+

Or render $items as YAML:

WP_CLI\Utils\format_items( 'yaml', $items, array( 'key', 'value' ) );

# ---
# -
#   key: foo
#   value: bar

No Hooks.

Return

null. Nothing (null).

Usage

format_items( $format, $items, $fields );
$format(string) (required)
Format to use: 'table', 'json', 'csv', 'yaml', 'ids', 'count'.
$items(array) (required)
An array of items to output.
$fields(array|string) (required)
Named fields for each item of data. Can be array or comma-separated list.

format_items() code WP-CLI 2.8.0-alpha

function format_items( $format, $items, $fields ) {
	$assoc_args = compact( 'format', 'fields' );
	$formatter  = new Formatter( $assoc_args );
	$formatter->display_items( $items );
}