我有一个查询 (+php) 用于获取父级的所有子级,但现在将其翻转,这样我得到一个元素的所有父级会更有用。
桌子:
ID parentID showOrder
===============================
1 0 0
2 1 0
3 2 0
4 3 1
5 3 2
6 3 3
代码:
<?php
$structure = array();
$sql = "SELECT ID, parentID FROM table ORDER BY parentID ASC, showOrder ASC";
while ($row = fetch_row()) {
$structure[$row['parentID']][] = $row['ID'];
}
输出:
0: [1], 1: [2], 2: [3], 3: [5,4,6]
首选结果:
0: [], 1: [0], 2: [1, 0], 3: [2, 1, 0],
4: [3, 2, 1, 0], 5: [3, 2, 1, 0], 6: [3, 2, 1, 0]
我怎样才能做到这一点?