我的问题与本主题中描述的问题非常相似从 PHP 数组为下拉选择字段创建嵌套列表问题是如果投资不连续,则错误地添加破折号
function buildTree(Array $data, $parent = 0) {
$tree = array();
foreach ($data as $d) {
if ($d['parent'] == $parent) {
$children = buildTree($data, $d['id']);
// set a trivial key
if (!empty($children)) {
$d['_children'] = $children;
}
$tree[] = $d;
}
}
return $tree;
}
function printTree($tree, $r = 0, $p = null) {
foreach ($tree as $i => $t) {
$dash = ($t['parent'] == 0) ? '' : str_repeat('-', $r) .' ';
printf("\t<option value='%d'>%s%s</option>\n", $t['id'], $dash, $t['name']);
if ($t['parent'] == $p) {
// reset $r
$r = 0;
}
if (isset($t['_children'])) {
printTree($t['_children'], ++$r, $t['parent']);
}
}
}
该解决方案适用于这样的结构数组
$rows = array(
array ('id' => 1, 'name' => 'Test 1', 'parent' => 0),
array ('id' => 2, 'name' => 'Test 1.1', 'parent' => 1),
array ('id' => 3, 'name' => 'Test 1.2', 'parent' => 1),
array ('id' => 4, 'name' => 'Test 1.2.1', 'parent' => 3),
array ('id' => 5, 'name' => 'Test 1.2.2', 'parent' => 3),
array ('id' => 6, 'name' => 'Test 1.2.2.1', 'parent' => 5),
array ('id' => 7, 'name' => 'Test 2', 'parent' => 0),
array ('id' => 8, 'name' => 'Test 2.1', 'parent' => 7),
);
但不适用于此
$rows = array(
array ('id' => 1, 'name' => 'Test 1', 'parent' => 0),
array ('id' => 2, 'name' => 'Test 1.1', 'parent' => 1),
array ('id' => 3, 'name' => 'Test 1.2', 'parent' => 1),
array ('id' => 4, 'name' => 'Test 1.2.1', 'parent' => 3),
array ('id' => 5, 'name' => 'Test 1.2.2', 'parent' => 3),
array ('id' => 6, 'name' => 'Test 1.2.2.1', 'parent' => 5),
array ('id' => 7, 'name' => 'Test 2', 'parent' => 0),
array ('id' => 8, 'name' => 'Test 2.1', 'parent' => 1),
array ('id' => 9, 'name' => 'another data', 'parent' => 1),
);
我怎样才能改变它?对不起我的英语不好