There is a comment on php.org at fetchAll page.
You might find yourself wanting to use
FETCH_GROUP
andFETCH_ASSOC
at the same time, to get your table's primary key as the array key:
// $stmt is some query like "SELECT rowid, username, comment"
$results = $stmt->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_ASSOC);
// It does work, but not as you might expect:
$results = array(
1234 => array(0 => array('username' => 'abc', 'comment' => '[...]')),
1235 => array(0 => array('username' => 'def', 'comment' => '[...]')), );
but you can at least strip the useless numbered array out easily:
$results = array_map('reset', $results);
The code functions as expected and everything is accomplished with just line:
array_map('reset', $results);
After reading documentation array_map and reset function I don't really understand how the result is produced when both are combined in a single line.
Is it a safe solution? Would you recommend this kind of one-line or is it a side effect and should not be used, i.e. should I write a plain old loop to produce the same result?
Single line solution combining two standard functions is a very attractive solution to me. I just want to make sure there are no surprises.