2
我经常需要检索结果并通过给定列访问它们。PHP约定检索由特定列索引的结果?
有没有一种方法可以在不需要每次遍历整个数据集的情况下编写它?
我研究了各种PDO获取模式,但没有比这更简单的跳出来。谢谢。
function get_groups() {
$stmt = $dbc->prepare("SELECT * FROM groups ORDER BY group_name");
$stmt->execute();
$groups = $stmt->fetchAll();
$return = [];
foreach($groups as $group) {
$return[$group['id']] = $group;
}
return $return;
}
你想要用作数组键的数据库ID? – 2014-02-10 20:20:38
[不,不是真的](http://stackoverflow.com/questions/5361716/is-there-a-way-to-fetch-associative-array-grouped-by-the-values-of-a-specified- c),除非你有一个简单的密钥对你想创建。 – Wrikken
@Wrikken,在你的链接问题中,我真的不明白什么建议不要将数据库唯一标识符作为数组键。谨慎解释一下为什么这样会很糟糕? –