sallon88 最近的时间轴更新
sallon88

sallon88

V2EX 第 30906 号会员,加入于 2012-12-19 15:49:23 +08:00
sallon88 最近回复了
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   ·   我们的愿景   ·   实用小工具   ·   953 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 11ms · UTC 20:37 · PVG 04:37 · LAX 13:37 · JFK 16:37
Developed with CodeLauncher
♥ Do have faith in what you're doing.