[wp-trac] [WordPress Trac] #31316: wp_list_pluck unexpectedly returns id indexed array instead of plucked values with index_key = null
WordPress Trac
noreply at wordpress.org
Fri Feb 13 04:32:35 UTC 2015
#31316: wp_list_pluck unexpectedly returns id indexed array instead of plucked
values with index_key = null
------------------------------------+------------------------------
Reporter: adamsilverstein | Owner:
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: General | Version: 3.1
Severity: normal | Resolution:
Keywords: has-patch dev-feedback | Focuses:
------------------------------------+------------------------------
Comment (by adamsilverstein):
Thanks for the input Boone. I understand the backwards compatibility
argument; and together you have certainly convinced me!
I guess its a feature, not a bug :) I will prepare a docblock patch to fix
the current incorrect implication.
An easy workaround if you always want the values only is to run the array
thru `array_values()` before wp_list_pluck.
Note that previous to r15686, the wp_filter_object_list function would
have only returned the values from an array (even if an index were
present), unless a $field were specified; when the code was split off into
wp_list_pluck (and wp_list_filter), it was changed to include the index
with the return (if present in the original array).
Replying to [comment:7 boonebgorges]:
> I came here to say that we probably can't change this for backward
compatibility reasons, but dd32 beat me to it :) I don't disagree that the
behavior is a bit odd with numeric keys, but it's very likely that there
are plugins that are expecting to have the keys preserved. In the case of
associative arrays, it would be bad indeed if this behavior were changed.
>
> It's worth noting that PHP's `array_filter()` preserves keys in the same
way as `wp_list_pluck()`, but `array_column()` does not.
>
> I do think it's worth adding a line to the docblock explaining the
current behavior.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/31316#comment:8>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list