Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从php数组创建html选择列表?_Php_Arrays_Select - Fatal编程技术网

从php数组创建html选择列表?

从php数组创建html选择列表?,php,arrays,select,Php,Arrays,Select,您好,我找到了从数组输出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

您好,我找到了从数组输出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>");
函数构建树(数组$data,$parent=0){
$tree=array();
foreach($d数据){
如果($d['parent']==$parent){
$children=buildTree($data,$d['id']);
//设置一个简单的键
如果(!空($children)){
$d[''u children']=$children;
}
$tree[]=$d;
}
}
返回$tree;
}
$rows=数组(
数组('id'=>1,'name'=>test1','parent'=>0),
数组('id'=>2,'name'=>test1.1,'parent'=>1),
数组('id'=>3,'name'=>test1.2,'parent'=>1),
数组('id'=>4,'name'=>test1.2.1,'parent'=>3),
数组('id'=>5,'name'=>test1.2.2,'parent'=>3),
数组('id'=>6,'name'=>test1.2.2.1,'parent'=>5),
数组('id'=>7,'name'=>test2','parent'=>0),
数组('id'=>8,'name'=>test2.1,'parent'=>7),
);
$tree=buildTree($rows);
//打印(树);
函数printTree($tree,$r=0,$p=null){
foreach($i=>t的树){
$dash=($t['parent']==0)?“”:str_重复('-',$r)。“”;
printf(“\t%s%s\n”、$t['id']、$dash、$t['name']);
如果($t['parent']=$p){
//重置$r
$r=0;
}
如果(isset($t[“U儿童])){
printTree($t['u children',+$r,$t['parent']);
}
}
}
打印(“\n”);
printree($tree);
打印(“”);
但我需要重写以返回如下结果:

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

echo $select;
// or better
return $select;
$select=”“;
$select.=打印树($list);
$select.=“”;
echo$select;
//或者更好
返回$select;
问题在于递归,解决方法是在数组中填充每个选项,但我不知道如何在递归函数中这样做,而且

printf("\t<option value='%d'>%s%s</option>\n", $t['id'], $dash, $t['name']);
printf(“\t%s%s\n”、$t['id']、$dash、$t['name']);
foreach循环迭代时直接打印


谢谢。

所以我找出了我的错误所在,这仅仅是因为我用html选项标记填充了一个数组

<option value="0">Start</option>
然后,如果需要输出HTML,请使用一些简单的循环

$html = '<select>';
foreach($result as $res){
    $html .= $res;  
}
$html .='</select>';
echo $html;
$html='';
foreach(结果为$res){
$html.=$res;
}
$html.='';
echo$html;
$aNonFlat = toSEL($list, 0, null, $currentID);

$result = array();
array_walk_recursive($aNonFlat,function($v, $k) use (&$result){ $result[] = $v; });
$html = '<select>';
foreach($result as $res){
    $html .= $res;  
}
$html .='</select>';
echo $html;