[php]代码库
$yArr = array(
1 => array('id'=>'1','parentid'=>0,'name'=>'一级栏目一'),
2 => array('id'=>'2','parentid'=>0,'name'=>'一级栏目二'),
3 => array('id'=>'3','parentid'=>1,'name'=>'二级栏目一'),
4 => array('id'=>'4','parentid'=>1,'name'=>'二级栏目二'),
5 => array('id'=>'5','parentid'=>2,'name'=>'二级栏目三'),
6 => array('id'=>'6','parentid'=>3,'name'=>'三级栏目一'),
7 => array('id'=>'7','parentid'=>3,'name'=>'三级栏目二'),
8 => array('id'=>'8','parentid'=>2,'name'=>'二级栏目三'),
);
/**
* 获取当前id的子ID
* @param array $data 原始数组
* @param int $id 当前id
* @param int $layer 当前层级
*/
function genCate($data, $pid = 0, $level = 0)
{
if($level == 10) break;
$l = str_repeat(" ", $level);
$l = $l.'└';
static $arrcat = array();
$arrcat = empty($level) ? array() : $arrcat;
foreach($data as $k => $row)
{
/**
* 如果父ID为当前传入的id
*/
if($row['parentid'] == $pid)
{
//如果当前遍历的id不为空
$row['name'] = $l.$row['name'];
$row['level'] = $level;
$arrcat[] = $row;
//var_array($arr);
genCate($data, $psiff, $row['id'], $level+1);//递归调用
}
}
return $arrcat;
}
$carr = genCate($yArr);
echo "<select>";
foreach($carr as $row)
{
echo "<option value={$row['id']}>";
echo $row['name'];
echo "</option>";
}
echo "</select>";