I often have a need for making a database query where the key of resulting array is the value of the first column specified in the SQL (instead of the key just being an auto-assigned incremental number as the array is being filled). Is that possible without having to rework the array manually afterwards?
I know there is queryAll(PDO::FETCH_KEY_PAIR) but that only works for two colums (the array value is a single database column instead of a sub-array of all the remaining columns)
So, instead of:
array (
0 =>
array (
'id' => 6955,
'firstname' => 'John',
'lastname' => 'Doe',
'country' => 'United States',
),
1 =>
array (
'id' => 8588,
'firstname' => 'Helen',
'lastname' => 'Smith',
'country' => 'Denmark',
),
)
...I need:
array (
6955 => array (
'firstname' => 'John',
'lastname' => 'Doe',
'country' => 'United States',
),
8588 => array (
'firstname' => 'Helen',
'lastname' => 'Smith',
'country' => 'Denmark',
),
)
It doesn't necessarily have to be arrays - it could made of objects as well. It is just the structure that is important.