-1

您好,我找到了从数组中输出 html 选择列表的 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;
}


$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),
);

$tree = buildTree($rows);
// print_r($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']);
    }
  }
}


print("<select>\n");
printTree($tree);
print("</select>");

但我需要重写以返回这样的结果:

$select = "<select>";
$select .= printTree($list);
$select .= "</select>";

echo $select;
// or better
return $select;

问题在于递归,解决方案是在数组中填充每个选项,但我不知道如何在递归函数中做到这一点,而且

printf("\t<option value='%d'>%s%s</option>\n", $t['id'], $dash, $t['name']);

当 foreach 循环迭代时直接打印。

谢谢。

4

1 回答 1

0

所以我弄清楚我的错误在哪里,这仅仅是因为我用 html 选项标签填充了一个数组,例如。

<option value="0">Start</option>

但是使用 php 函数 print_r() 当我检查 DOM 元素时,我在数组值 exepts 中什么也看不到。

所以这是我的最终解决方案:这个函数在多维数组中填充值,以进一步需要

# edited printTree() function, renamed to toSEL()
# $array - data array like above, 
# $r - to correct iterate, $p - parent id, 
# $currentID - what id is selected

function toSEL($array, $r = 0, $p = null, $currentID=null){
    foreach($array as $value){
        $dash = ($value[parent] == 0) ? '' : str_repeat('-', $r) .' ';
        if($value[id]==$currentID){
            $html[] = '<option value="'.$value[id].'" selected="selected">'.$dash.$value[name].'</option>';
        }else{
            $html[] = '<option value="'.$value[id].'">'.$dash.$value[name].'</option>';
        }

        if($value['parent'] == $p){
            // reset $r
            $r = 0;
        }
        if(!empty($value[children])){
            $html[] = toSEL($value[children], ++$r, $value[parent], $currentID);

        }

    }

    return $html;
}

从多维数组转换为一维

$aNonFlat = toSEL($list, 0, null, $currentID);

$result = array();
array_walk_recursive($aNonFlat,function($v, $k) use (&$result){ $result[] = $v; });

然后如果需要输出 HTML 使用一些简单的循环。

$html = '<select>';
foreach($result as $res){
    $html .= $res;  
}
$html .='</select>';
echo $html;
于 2013-11-05T02:43:29.443 回答