Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP PDO grouping results
<?php
$stmt = $pdo_connection->query('SELECT id, name, surname FROM people');
$people = $stmt->fetchAll(\PDO::FETCH_GROUP | \PDO::FETCH_UNIQUE | \PDO::FETCH_ASSOC);
/*
$people will now contain:
array(
1 => array(
'name' => 'Juan',
'surname' => 'Sorin',
),
...
)
if you omit \PDO::FETCH_UNIQUE, array of arrays will returned
(useful if multiple rows can have same id)
array(
1 => array(
0 => array(
'name' => 'Juan',
'surname' => 'Sorin',
),
1 => ...
),
...
)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment