function search(&$node, &$forest, $id)
{
$node = null;
$len = count($forest);
for ($i=0; $i<$len; ++$i)
{
if ($forest[$i]['id'] == $id)
{
$node = &$forest[$i]; //【去掉“&”就能找到,加上就找不到】
return;
}
search($node, $forest[$i]['children'], $id);
if ($node) { return; }
}
}
$forest = array(
array(
'id' => 1,
'pid' => 0,
'children' => array(
array(
'id' => 11,
'pid' => 1,
'children' => array(),
),
),
),
array(
'id' => 2,
'pid' => 0,
'children' => array(),
),
);
search($node, $forest, 11);
echo json_encode($node);
1 回答
- 1 回答
- 0 关注
- 410 浏览
添加回答
举报
0/150
提交
取消