如何使用循环从数据库结果集中在 PHP 中创建如下所示的数组:
Array
(
[T] => Array
(
[0] => Array
(
[id] => 1
[name] => Timer
)
[1] => Array
(
[id] => 2
[name] => Tub
)
)
[P] => Array
(
[0] => Array
(
[id] => 3
[name] => Paper
)
[1] => Array
(
[id] => 4
[name] => Puppy
)
)
)
您会注意到数组键是一个字母,它取自结果集中的“名称”值。循环将是这样的:
while($result = $db->fetch($query) {
$key = $result['name']{0};
// your answer :-)
}