function get_categories_tree()
{
$sql="select cat_id as id,parent_id as pid,cat_name as name from shopdb_category order by sort_order asc ";
$ar=$GLOBALS['db']->getAll($sql);
$index = array();
$tree = array();
$pid = 0;
$j = 0;
$s = 0;
$k = 0;
$i = 0;
$isordered = false; //如果原数据已经按层级排序,则按最优算法处理,否则,按默认算法处理
$ar_size = count($ar);
while($i < $ar_size)
{
if($ar[$i]['pid'] == $pid && empty($ar[$i]['yes']))
{
$index[$j]['index'] = $i;
$index[$j]['pid'] = $pid;
if($pid == 0)
{
eval('$counts = count($tree);');
$index[$j]['index_str'] = "[". $counts. "]['children']";
$j++;
eval( '$tree[count($tree)] = $ar[$i];' );
eval( '$tree[count($tree)-1][\'children\'] = array();' );
}
else
{
$str = $index[$j-1]['index_str'];
eval('$counts = count($tree'. $str. ');');
eval( '$tree'. $str. '[$counts] = $ar[$i];' );
eval( '$tree'. $str. '[$counts][\'children\'] = array();' );
$index[$j]['index_str'] = $index[$j-1]['index_str']. "[". $counts. "]['children']";
$j++;
}
$ar[$i]['yes'] = 1;
$pid = $ar[$i]['id'];
$s++;
if($isordered == false)
{
$i = -1;
}
}
$i++;
if($i > $ar_size-1)
{
$i = ($is_ordered == false) ? 0

index[$j-1]['index'];
$pid = $index[$j-1]['pid'];
array_pop($index);
$j--;
}
if($s >= $ar_size)
{
break;
}
$k++;
if( $k >= $ar_size*($ar_size+1) )
{
break;
}
}
return $tree;
}
模版自己改啦!哈哈