Be careful when using PDO::FETCH_COLUMN with PDO::FETCH_GROUP. By default, results are grouped by first column (index 0) and second column (index 1) is returned. But, if you provide fetch argument, it wouldn't affect returned column, but grouping column. If grouping column is set explicitly , first columns is returned instead of second.
<?php
$insert = $dbh->prepare("INSERT INTO people(id, gender) VALUES (?, ?)");
$insert->execute(array('2', 'female'));
$insert->execute(array('3', 'female'));
$insert->execute(array('4', 'female'));
$insert->execute(array('5', 'male'));
$insert->execute(array('6', 'male'));
$sth = $dbh->prepare("SELECT gender, id FROM people");
$sth->execute();
var_dump($sth->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP));
?>
Returns:
<?php
array (size=2)
'female' =>
array (size=3)
0 => string '2' (length=1)
1 => string '3' (length=1)
2 => string '4' (length=1)
'male' =>
array (size=2)
0 => string '5' (length=1)
1 => string '6' (length=1)
?>
But,
<?php
var_dump($sth->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP, 0));
?>
returns:
<?php
array (size=2)
'female' =>
array (size=3)
0 => string 'female' (length=1)
1 => string 'female' (length=1)
2 => string 'female' (length=1)
'male' =>
array (size=2)
0 => string 'male' (length=1)
1 => string 'male' (length=1)
?>
and
<?php
var_dump($sth->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP, 1));
?>
returns
<?php
array (size=5)
2 =>
array (size=1)
0 => string 'female' (length=1)
3 =>
array (size=1)
0 => string 'female' (length=1)
4 =>
array (size=1)
0 => string 'female' (length=1)
5 =>
array (size=1)
0 => string 'male' (length=1)
6 =>
array (size=1)
0 => string 'male' (length=1)
?>
First column is retuned and grouping is done by provided column index.