PHP MySQL PDO结果是递归关联数组?

时间:2013-12-20 19:24:31

标签: php mysql pdo iteration

我有一个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实现相同的最终结果,而无需手动迭代?

0 个答案:

没有答案