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
(
...
)
)
)
To strip the result of the useless numbered array I used
$objects = array_Map('reset', $objects);
As described here:
https://www.php.net/manual/en/pdostatement.fetchall.php#88699
or here:
https://gist.github.com/betweenbrain/9239337
In PHP 8.2 I get a warning:
PHP Warning: reset(): Argument #1 ($array) must be passed by reference, value given
What other way can I use to get 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
(
...
)
)
2
Answers
There’s no need to map over the array. Add the
PDO::FETCH_UNIQUE
flag and the result will have each element as a single object, rather than an array of objects.See this example in the documentation.
When you pass
reset
directly as the callback function toarray_map
, you are attempting to usereset
on each element of the$objects
array. However,reset
expects its argument to be passed by reference because it potentially modifies the array by resetting its internal pointer to the first element. So you can use either anonymous function (closure) withreset
inside it orcurrent
which does not require passing array by reference.output:
$objects
:$new_array
(usingreset
orcurrent
: