Opened 12 years ago
Closed 12 years ago
#28666 closed enhancement (fixed)
Index key support for wp_list_pluck(), à la array_column()
| Reported by: | trepmal | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | 4.0 |
| Component: | General | Version: | |
| Severity: | normal | Keywords: | |
| Cc: | Focuses: |
Description
PHP 5.5's array_column() has a neat third argument for specifying a field to use as the key for the new array. Patch adds a third optional argument to wp_list_pluck() that mimics that behavior.
Current functionality is this:
wp_list_pluck( get_posts( ), 'post_title' );
which returns an array like
Array
(
[0] => Hello World!
[1] => Markup: HTML Tags and Formatting
[2] => Markup: Image Alignment
[3] => Markup: Text Alignment
[4] => Markup: Title With Special Characters ~`!@#$%^&*()-_=+{}[]/;:'"?,.>
)
With the patch, you could specify a field to use as the new keys, so
wp_list_pluck( get_posts( ), 'post_title', 'ID' );
would return
Array
(
[1699] => Hello World!
[1178] => Markup: HTML Tags and Formatting
[1177] => Markup: Image Alignment
[1176] => Markup: Text Alignment
[1174] => Markup: Title With Special Characters ~`!@#$%^&*()-_=+{}[]/;:'"?,.>
)
Attachments (2)
Change History (8)
#3
@
12 years ago
- Keywords needs-patch added; has-patch removed
- Milestone Awaiting Review → 4.0
To optimize the speed of this as much as possible, can we put the if conditional outside of two different foreach loops? We should also reduce the is_object() calls to once per foreach iteration.
We should include in the description for this function that this is an implementation of array_column() that also works with objects.
I wish it only supported all objects or all arrays. As written, it does an is_object() each time. If we somehow knew it was an array of arrays, we could just have this wrap array_column() and get the performance benefits of C.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
Neat-o. +1 for this.