Codex tools: Log in
Contents |
Pluck a certain field out of each object in a list
<?php wp_list_pluck( $list, $field ); ?>
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' ); ?>
Since: 3.1
wp_list_pluck() is located in wp-includes/functions.php