sallon88's recent timeline updates
sallon88

sallon88

V2EX member #30906, joined on 2012-12-19 15:49:23 +08:00
sallon88's recent replies
Dec 19, 2012
Replied to a topic by 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>
About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3258 Online   Highest 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 13ms · UTC 11:58 · PVG 19:58 · LAX 04:58 · JFK 07:58
♥ Do have faith in what you're doing.