Make WordPress Core

Opened 10 years ago

Closed 10 years ago

#27765 closed enhancement (wontfix)

Add ARRAY_K return type to wpdb::get_results()

Reported by: jdgrimes's profile 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)

27765.diff (1.2 KB) - added by wonderboymusic 10 years ago.

Download all attachments as: .zip

Change History (9)

#1 follow-up: @pento
10 years ago

Can you give an example of a use case for this? Particularly, one that isn't covered by OBJECT_K or ARRAY_A?

#2 @SergeyBiryukov
10 years ago

  • Version changed from trunk to 3.8

#3 @wonderboymusic
10 years ago

  • Keywords has-patch added
  • Milestone changed from Awaiting Review to 4.0

maybe?

#4 in reply to: ↑ 1 @jdgrimes
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 or ARRAY_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.

#5 @pento
10 years ago

  • Keywords commit added

#6 follow-up: @nacin
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.

#7 in reply to: ↑ 6 @jdgrimes
10 years ago

  • Keywords close added

Replying to nacin:

I hadn't thought of DB drop-ins. Because of that, it isn't really worth it.

#8 @wonderboymusic
10 years ago

  • Milestone 4.0 deleted
  • Resolution set to wontfix
  • Status changed from new to closed

yeah - use, OBJECT_K and do $row = (array) $row in your loop-type things

Note: See TracTickets for help on using tickets.