[wp-trac] [WordPress Trac] #56262: WP_List_Util::pluck doesn't support a null $field

WordPress Trac noreply at wordpress.org
Wed Jul 20 20:28:53 UTC 2022


#56262: WP_List_Util::pluck doesn't support a null $field
-------------------------+-----------------------------
 Reporter:  iandunn      |      Owner:  (none)
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  General      |    Version:  3.1
 Severity:  normal       |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 [https://www.php.net/manual/en/function.array-column.php array_column()]
 accepts a `null` value for the `$column_key` parameter. When passed
 `null`, it returns the complete array, which is a useful trick for
 reindexing database results, etc.

 {{{#!php
 $array = [
     [ 'id' => 123, 'name' => 'Joe' ],
     [ 'id' => 345, 'name' => 'Sally' ]
 ];

 array_column( $array, null, 'id' ); // [ 123 => [ 'id' => 123, 'name' =>
 'Joe'] ...
 }}}

 `wp_list_pluck()` doesn't work when using that on an object, though:

 > Warning: Undefined property: stdClass::$ in wp-includes/class-wp-list-
 util.php on line 185


 It seems like `array_column()` has had support for objects since PHP7, so
 one way to achieve this might be something like:

 {{{#!php
 if ( version_compare( ... ) {
     return array_column( $this->output, $field, $index_key )
 }

 // existing code remains as fallback for older versions
 }}}

 It might be good to use something like `7.0.7` to skip a few bugs that
 were fixed.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/56262>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list