【发布时间】:2012-08-14 21:43:23
【问题描述】:
我使用这个脚本来创建具有层次类别的数组:
$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 级)
这里是来源:
【问题讨论】: