WordPress.org

Ready to get started?Download WordPress

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/wp list pluck

Contents

Description

Pluck a certain field out of each object in a list

Usage

<?php wp_list_pluck$list$field$index_key null ); ?>

Parameters

$list
(array) (required) A list of objects or arrays
Default: None
$field
(mixed) (required) A field from the object to place instead of the entire object
Default: None
$index_key
(mixed) (optional) Field from the object to use as keys for the new array
Default: null

Return Values

(array) 
Array of found values. If $index_key is set, an array of found values with keys corresponding to $index_key.

Examples

The following is an array listing foods:

<?php
$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().

<?php $food_names = wp_list_pluck( $foods, 'name' ); ?>

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

<?php
array(
	'Banana',
	'Apple',
	'Lettuce',
	'Apple'
);
?>

Change Log

Since: 3.1

Source File

wp_list_pluck() is located in wp-includes/functions.php