Opened 10 years ago
Closed 10 years ago
#27765 closed enhancement (wontfix)
Add ARRAY_K return type to wpdb::get_results()
Reported by: | jdgrimes | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 3.8 |
Component: | Database | Keywords: | has-patch close |
Focuses: | Cc: |
Description
This is a request to add the ARRAY_K
return type to wpdb::get_results()
to compliment the OBJECT_K
type (#5286). This would cause the method to return an array of associative arrays indexed by the value of the first column. In other words, like ARRAY_A
except indexed by the first column's value.
Array ( ['value_1'] => Array ( ['col_1'] => ['value_1'] ['col_2'] => ['value_2'] ) ... )
I've thought to myself on multiple occasions that this would have been nice to have. Apparently nobody else has though (or didn't think it was worth a ticket), so maybe there really aren't that many use-cases for it.
Attachments (1)
Change History (9)
#4
in reply to:
↑ 1
@
10 years ago
Replying to pento:
Can you give an example of a use case for this? Particularly, one that isn't covered by
OBJECT_K
orARRAY_A
?
Well, anytime you have built the code around arrays, and then realize it would have been much better if you could have had them indexed by the first column. Admittedly, it in most (perhaps all?) cases it is possible to do it with OBJECT_K
, but you may already be expecting arrays throughout your code. Basically, it's for convenience, like the other formats.
#6
follow-up:
↓ 7
@
10 years ago
- Keywords commit removed
Potential downside: Most DB drop-ins have only implemented OBJECT_K, ARRAY_A, ARRAY_N, and OBJECT. The newest one, OBJECT_K, was still added a solid five years ago. There's no way of knowing whether a drop-in supports the newer ARRAY_K, and using it in your code suddenly means things will fatal error (accessing an object as an array), when all you wanted was a quick shortcut.
OBJECT_K is awesome but it comes up pretty rarely in practice. I probably use it more than anyone. Is making this more complex necessary and worth it? You can always do this cast yourself in your loop.
Can you give an example of a use case for this? Particularly, one that isn't covered by
OBJECT_K
orARRAY_A
?