I use PDOStatement::fetchAll with FETCH_GROUP and FETCH_ASSOC to get my table's primary key as the array key.
$objects = $stmt_obj->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_CLASS);
This leads to the following result:
Array
(
[56] => Array
(
[0] => stdClass Object
(
...
)
)
[39] => Array
(
[0] => stdClass Object
(
...
)
)
[38] => Array
(
[0] => stdClass Object
(
...
)
)
[71] => Array
(
[0] => stdClass Object
(
...
)
)
)
What can I do to get rid of the useless numbered array and get the following result:
Array
(
[56] => stdClass Object
(
...
)
[39] => stdClass Object
(
...
[38] => stdClass Object
(
...
)
[71] => stdClass Object
(
...
)
)
There's no need to map over the array. Add the
PDO::FETCH_UNIQUEflag and the result will have each element as a single object, rather than an array of objects.See this example in the documentation.