wp_list_pluck

Pluck a certain field out of each object in a list.

Description

(array) wp_list_pluck( (array) $list, (int|string) $field, (null) $index_key = null ); 

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

Returns (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 `$list` will be preserved in the results.

Parameters (3)

0. $list (array)
List of objects or arrays
1. $field (int|string)
Field from the object to place instead of the entire object
2. $index_key — Optional. (null) => null
Field from the object to use as keys for the new array. Default null.

Usage

  1. if ( !function_exists( 'wp_list_pluck' ) ) { 
  2. require_once ABSPATH . WPINC . '/functions.php'; 
  3.  
  4. // List of objects or arrays 
  5. $list = array(); 
  6.  
  7. // Field from the object to place instead of the entire object 
  8. $field = null; 
  9.  
  10. // Optional. Field from the object to use as keys for the new array. 
  11. // Default null. 
  12. $index_key = null; 
  13.  
  14. // NOTICE! Understand what this does before running. 
  15. $result = wp_list_pluck($list, $field, $index_key); 
  16.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/functions.php  
  1. function wp_list_pluck( $list, $field, $index_key = null ) { 
  2. $util = new WP_List_Util( $list ); 
  3. return $util->pluck( $field, $index_key );