<?php
$arr = array(
array(
'id' => 1,
'pid' => 0,
'name' => 'A'
),
array(
'id' => 2,
'pid' => 0,
'name' => 'A'
),
array(
'id' => 3,
'pid' => 1,
'name' => 'A'
),
array(
'id' => 4,
'pid' => 0,
'name' => 'A'
),
array(
'id' => 5,
'pid' => 4,
'name' => 'A'
),
array(
'id' => 6,
'pid' => 5,
'name' => 'A'
),
);
function list_to_tree($list, $pid = 0)
{
$res = array();
if (!empty($list)) {
foreach ($list as $k => &$v) {
if ($v['pid'] == $pid) {
$v['child'] = list_to_tree($list, $v['id']);
$res[] = $v;
}
}
unset($v);
}
return $res;
}
function getChildren($parent, $deep=0) {
foreach($parent as $row) {
$data[] = array("id"=>$row['id'], "name"=>$row['name'],"pid"=>$row['pid'],'deep'=>$deep);
if (!empty($row['child'])) {
$data = array_merge($data, getChildren($row['child'], $deep+1));
}
}
return $data;
}
$options = getChildren(list_to_tree($arr));
?>
<select name="" id="">
<?php foreach ($options as $row) { ?>
<option value="<?php echo $row['id'] ?>"><?php echo str_pad("",$row['deep']*3, "|----",STR_PAD_RIGHT); ?><?php echo $row['name']; ?></option>
<?php } ?>
</select>
转载请注明原文地址: https://www.6miu.com/read-4069.html