wc_list_pluck

Based on wp_list_pluck, this calls a method instead of returning a property.

Description

(array) wc_list_pluck( (array) $list, (int|string) $callback_or_field, (null) $index_key = null ); 

Returns (array)

Array of values.

Parameters (3)

0. $list (array)
List of objects or arrays
1. $callback_or_field (int|string)
Callback method 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( 'wc_list_pluck' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-core-functions.php'; 
  3.  
  4. // List of objects or arrays 
  5. $list = array(); 
  6.  
  7. // Callback method from the object to place instead of the entire object 
  8. $callback_or_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 = wc_list_pluck($list, $callback_or_field, $index_key); 
  16.  

Defined (1)

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

/includes/wc-core-functions.php  
  1. function wc_list_pluck( $list, $callback_or_field, $index_key = null ) { 
  2. // Use wp_list_pluck if this isn't a callback 
  3. $first_el = current( $list ); 
  4. if ( ! is_object( $first_el ) || ! is_callable( array( $first_el, $callback_or_field ) ) ) { 
  5. return wp_list_pluck( $list, $callback_or_field, $index_key ); 
  6. if ( ! $index_key ) { 
  7. /** 
  8. * This is simple. Could at some point wrap array_column() 
  9. * if we knew we had an array of arrays. 
  10. */ 
  11. foreach ( $list as $key => $value ) { 
  12. $list[ $key ] = $value->{$callback_or_field}(); 
  13. return $list; 
  14.  
  15. /** 
  16. * When index_key is not set for a particular item, push the value 
  17. * to the end of the stack. This is how array_column() behaves. 
  18. */ 
  19. $newlist = array(); 
  20. foreach ( $list as $value ) { 
  21. if ( isset( $value->$index_key ) ) { 
  22. $newlist[ $value->$index_key ] = $value->{$callback_or_field}(); 
  23. } else { 
  24. $newlist[] = $value->{$callback_or_field}(); 
  25. return $newlist;