WP-CLI 命令行手册

title: "WP_CLI\Utils\format_items()" post_status: publish comment_status: open taxonomy: category: - wp-cli-handbook post_tag: - Internal Api - Repos - Data


WP_CLI\Utils\format_items()

将项目集合渲染为 ASCII 表格、JSON、CSV、YAML、ID 列表或计数。


用法

WP_CLI\Utils\format_items( $format, $items, $fields )
$format (string) 使用的格式:'table'、'json'、'csv'、'yaml'、'ids'、'count'。
$items (array<mixed>) 要输出的项目数组。
$fields (array<string>|string) 每个数据项的命名字段。可以是数组或逗号分隔的列表。

说明

给定一组数据结构一致的条目:

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

$items 渲染为 ASCII 表格:

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

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

或将 $items 渲染为 YAML:

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

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

内部 API 文档会在每次发布时从 WP-CLI 代码库生成。如需改进,请提交一个拉取请求。

相关