Skip to content

Instantly share code, notes, and snippets.

@sobstel
Created May 31, 2011 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sobstel/1001068 to your computer and use it in GitHub Desktop.
Save sobstel/1001068 to your computer and use it in GitHub Desktop.
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