我使用此脚本创建具有层次结构类别的数组:
$refs = array();
$list = array();
$sql = "SELECT item_id, parent_id, name FROM items ORDER BY name";
$result = mysql_query($sql);
while($data = @mysql_fetch_assoc($result)) {
$thisref = &$refs[ $data['item_id'] ];
$thisref['parent_id'] = $data['parent_id'];
$thisref['name'] = $data['name'];
if ($data['parent_id'] == 0) {
$list[ $data['item_id'] ] = &$thisref;
} else {
$refs[ $data['parent_id'] ]['children'][ $data['item_id'] ] = &$thisref;
}
}
如何获得元素数组的级别?这是示例:
- 猫 A (0 级)
- 子类别 1 (1 级)
- Sub_Sub_Cat 1 (2 级)
- Sub_Sub_Cat 2 (2 级)
- Sub_Cat 2 (1 级)
- 子类别 1 (1 级)
- 猫 B (0 级)
- 猫 C (0 级)
这是来源: