我有一个MySQL
表:
+---------+------------------------+-----------+
| topicID | questionTopics_topicID | topicName |
+---------+------------------------+-----------+
| 1 | 1 | Root |
| 2 | 1 | Colors |
| 3 | 2 | Black |
| 4 | 2 | White |
| 5 | 1 | Animals |
| 6 | 5 | Dog |
| 7 | 5 | Cat |
+---------+------------------------+-----------+
我想要一个PHP Array
,其中结果集被提取为questionTopics_topicID
作为父项:
$array = array(
"Animals" => array(
6 => "Dog",
7 => "Cat"
),
"Colors" => array(
3 => "Black",
4 => "White"
),
);
目前我通过迭代2次结果集来做,首先添加main-categories
(颜色,动物),然后添加sub-categories
(狗,白色)。
有没有办法用PDO
实现相同的最终结果,而无需手动迭代?