Opened 3 years ago
Last modified 3 years ago
#56262 new enhancement
WP_List_Util::pluck doesn't support a null $field
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Awaiting Review | Priority: | low |
| Severity: | minor | Version: | 3.1 |
| Component: | General | Keywords: | has-patch needs-testing |
| Focuses: | Cc: |
Description
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.
$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:
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.
Attachments (1)
Change History (4)
Note: See
TracTickets for help on using
tickets.
56262.diff looks like a good start!
Two helpful improvements could be adding unit tests and inline docs (adding a
@since, updating the$fieldparameter description, etc).is_null( $field )might be more explicit / self documenting thanisset(), especially after the$fielddocs are updated to mention thatnullcan be passed.Using
isset()could also unintentionally hide PHP warnings (and therefore bugs) when devs make a typo in the field name, etc.