函数文档

wp_list_pluck()

💡 云策文档标注

概述

wp_list_pluck() 是 WordPress 核心函数,用于从数组中的每个对象或数组中提取指定字段,功能类似于 PHP 5.5 的 array_column(),但支持对象。它通过 WP_List_Util 类实现,常用于简化数据提取操作。

关键要点

  • 函数原型:wp_list_pluck( $input_list, $field, $index_key = null ),其中 $input_list 为对象或数组列表,$field 为要提取的字段,$index_key 可选用于指定新数组的键。
  • 返回值:返回提取值的数组;若 $index_key 设置,则返回以 $index_key 值为键的关联数组;若 $index_key 为 null,则保留原数组键。
  • 与 array_column() 的区别:wp_list_pluck() 不检查 $field 是否存在,若缺失会添加 null 并可能生成警告,而 array_column() 会跳过缺失项。
  • 使用场景:广泛用于 WordPress 核心功能,如处理文章、评论、媒体对象等,提取特定属性(如 ID、名称)以构建新数组。

代码示例

$foods = array(
    array(
        'name'  => 'Banana',
        'color' => 'Yellow',
    ),
    array(
        'name'  => 'Apple',
        'color' => 'Red',
    ),
);
$food_names = wp_list_pluck( $foods, 'name' ); // 返回 array('Banana', 'Apple')

注意事项

  • 使用 $index_key 时,确保字段值唯一,否则可能覆盖数据(如相同键值导致后值覆盖前值)。
  • 若 $field 不存在于某些项中,函数会添加 null 并可能触发警告,需注意错误处理。
  • 当 $index_key 值为数组或对象时,会抛出 TypeError,可能导致页面中断,建议提前验证数据。

📄 原文内容

Plucks a certain field out of each object or array in an array.

Description

This has the same functionality and prototype of array_column() (PHP 5.5) but also supports objects.

Parameters

$input_listarrayrequired
List of objects or arrays.
$fieldint|stringrequired
Field from the object to place instead of the entire object.
$index_keyint|stringoptional
Field from the object to use as keys for the new array.

Default:null

Return

array Array of found values. If $index_key is set, an array of found values with keys corresponding to $index_key. If $index_key is null, array keys from the original $input_list will be preserved in the results.

Source

function wp_list_pluck( $input_list, $field, $index_key = null ) {
	if ( ! is_array( $input_list ) ) {
		return array();
	}

	$util = new WP_List_Util( $input_list );

	return $util->pluck( $field, $index_key );
}

Changelog

Version Description
4.7.0 Uses WP_List_Util class.
4.0.0 $index_key parameter added.
3.1.0 Introduced.

User Contributed Notes

  1. Skip to note 4 content

    This is an example of how to use this function.

    The following is an array listing foods:

    $foods = array(
    	array(
    		'name'  => 'Banana',
    		'color' => 'Yellow',
    	),
    	array(
    		'name'  => 'Apple',
    		'color' => 'Red',
    	),
    	array(
    		'name'  => 'Lettuce',
    		'color' => 'Green',
    	),
    	array(
    		'name'  => 'Apple',
    		'color' => 'Red',
    	),
    );

    The names of each food can easily be “plucked” from the $foods array using wp_list_pluck() .

    $food_names = wp_list_pluck( $foods, 'name' ); 

    $food_names will now contain a numerically indexed array of food names equivalent to:

    array(
    	'Banana',
    	'Apple',
    	'Lettuce',
    	'Apple'
    );

  2. Skip to note 5 content

    Use of the $index_key argument is best reserved for unique or index fields, otherwise data could be missing from the returned array. Consider this modified $foods list from the previous example:

    $foods = array(
        array(
            'name'  => 'Banana',
            'color' => 'Yellow',
        ),
        array(
            'name'  => 'Apple',
            'color' => 'Red',
        ),
        array(
            'name'  => 'Kiwi',
            'color' => null,
        ),
        array(
            'name'  => 'Lettuce',
            'color' => 'Green',
        ),
        array(
            'name'  => 'Cherry',
            'color' => 'Red',
        ),
    );

    Using ‘color’ as $index_key is questionable.

    $food_names = wp_list_pluck( $foods, 'name', 'color' );

    $food_names contains an associative array equivalent to:

    array(
        'Yellow' => 'Banana',
        'Red'    => 'Cherry',
         0       => 'Kiwi',
        'Green'  => 'Lettuce',
    )

    The ‘Apple’ value was overwritten by the subsequent ‘Cherry’ since they both had a ‘Red’ index key value. Since ‘Kiwi’ had an unusable color value, its key is simply indexed.

  3. Skip to note 6 content

    This is functionally identical to the built-in (as of PHP 5.5) function array_column(), except that wp_list_pluck() does NOT check if $field is a key/property in each item of $input_list. If the key/property doesn’t exist in an item, null will be added to the new array and a warning will be generated, whereas array_column() simply won’t add a value at all. If you prefer the former behavior, just be aware that use of this function could lead to warnings being output (depending on whether WordPress or your server environment is configured to display errors). Otherwise you should use array_column(). Also, if you use $index_key and an item’s value at $index_key is an array or object then a TypeError will be thrown, bringing the whole page to a halt if it isn’t caught (same as with array_column()).