handbook/references/internal-api/wp-cli-utils-format-items.md
Pascal Birchler 264cb131bf
Fix handbook information architecture
- Organize files in folders according to the desired structure
- Use directory iterator to loop through all files and folders (makes generation more robust)
2025-06-25 15:38:39 +02:00

3.1 KiB

WP_CLI\Utils\format_items()

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


Usage

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

Notes

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

Internal API documentation is generated from the WP-CLI codebase on every release. To suggest improvements, please submit a pull request.