V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  sallon88  ›  全部回复第 1 页 / 共 1 页
回复总数  1
2012-12-19 16:12:53 +08:00
回复了 iloveyou 创建的主题 PHP 算法苦逼,求一个最佳方案
不就是无限分类么,曾经看到过有人使用引用做的,大概是这个样子
<pre>
function get_tree($items)
{
$tree = array(); //格式化好的树
foreach ($items as $item)
if (isset($items[$item['parent_id']]))
$items[$item['parent_id']]['child'][] = &$items[$item['id']];
else
$tree[] = &$items[$item['id']];
return $tree;
}
$items = array(
1 => array('id'=>1, 'title'=>'title1', 'parent_id'=>0),
2 => array('id'=>2, 'title'=>'title2', 'parent_id'=>0),
3 => array('id'=>3, 'title'=>'title3', 'parent_id'=>1),
4 => array('id'=>4, 'title'=>'title4', 'parent_id'=>2)
);
echo json_encode(get_tree($items));
</pre>
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   819 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 11ms · UTC 22:32 · PVG 06:32 · LAX 15:32 · JFK 18:32
Developed with CodeLauncher
♥ Do have faith in what you're doing.