我们可以使用PDO::FETCH_COLUMN | PDO::FETCH_GROUP
这样的
city_id temp date
1 24.3 2017-12-03
1 35.2 2017-11-26
1 24.4 2017-11-19
1 28.0 2017-11-12
2 4.0 2017-12-03
2 2.0 2017-11-26
2 6.0 2017-11-19
2 7.0 2017-11-12
.
.
$stmt = $pdo->query("SELECT `city_id`,`temp` FROM cities");
$rows = $stmt->fetchAll(PDO::FETCH_COLUMN | PDO::FETCH_GROUP);
$rows
将是这样的
[
1 => [24.3,35.2,24.4,28.0],
2=> [4.0,2.0,6.0,7.0],
....
]
但是有可能以某种方式获得一个关联的多维数组,其中将代表所有三列。像这样的东西
$stmt = $pdo->query("SELECT `city_id`,date,`temp` FROM cities");
$rows = $stmt->fetchAll(What should be here? so that);
$rows
应该是这样的
[
1 => [2017-12-03 => 24.3, 2017-11-26=>35.2, 2017-11-19=>24.4, 2017-11-12=>28.0],
2=> [2017-12-03 => 4.0, 2017-11-26=>2.0, 2017-11-19=>6.0, 2017-11-12=>7.0],
....
]