[wp-trac] [WordPress Trac] #59774: Undefined array key when using wp_list_pluck function

WordPress Trac noreply at wordpress.org
Mon Jan 22 14:46:20 UTC 2024


#59774: Undefined array key when using wp_list_pluck function
-------------------------------------------------+-------------------------
 Reporter:  iamarunchaitanyajami                 |       Owner:  (none)
     Type:  defect (bug)                         |      Status:  new
 Priority:  normal                               |   Milestone:  6.5
Component:  Script Loader                        |     Version:  5.1
 Severity:  normal                               |  Resolution:
 Keywords:  has-patch needs-unit-tests has-      |     Focuses:
  testing-info                                   |
-------------------------------------------------+-------------------------

Comment (by david.binda):

 Looking into the function and related PHPUnit tests, my understanding of
 the `wp_list_pluck` implementation is that it should mimic the
 `array_column` behaviour for both, arrays and objects.

 And thus my assumption is that we should not add an arbitrary value,
 `array()`, to a non-existing key as is being proposed, as the
 `array_column` simply skips the entry with a missing key. Eg.:

 {{{#!php
 <?php
 $input_list = array(
     array( '123' => '456' ),
     array( 'foo' => 'bar' ),
     array( 'foo' => 'baz' ),
 );

 var_dump( array_column( $input_list, 'foo', null ) );
 }}}

 produces:


 {{{
 array(2) {
   [0]=>
   string(3) "bar"
   [1]=>
   string(3) "baz"
 }
 }}}

 I'm attaching another version of the patch as well as PHPUnit tests. While
 writting the PHPUnit tests, I have noticed another PHP Warning which would
 occur in case the `$index_key` param of the `wp_list_pluck` is not `null`,
 but the `$field` is missing in one of the `$input_field`s. I have modified
 existing PHPUnit tests to cover such a situation.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/59774#comment:5>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list