html作为php变量未正确呈现

时间:2014-01-22 12:23:39

标签: php html var

有以下方法

  public function printDropdownTree($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'])) {
                $this->printDropdownTree($t['_children'], ++$r, $t['parent']);
            }
        }
    }

打印出嵌套的选择选项,并且工作正常。但我想将结果作为变量返回,我试图实现这一点,如

  public function printDropdownTree($tree, $r = 0, $p = null) {
        $html = "";
        foreach ($tree as $i => $t) {
            $dash = ($t['parent'] == 0) ? '' : str_repeat('-', $r) . ' ';
            $html .= '<option value="'.$t['id'].'">'.$dash.''.$t['name'].'</option>';
            if ($t['parent'] == $p) {
                // reset $r
                $r = 0;
            }
            if (isset($t['_children'])) {
                $this->printDropdownTree($t['_children'], ++$r, $t['parent']);
            }
        }


         return $html;
    }

$dash将无法呈现

1 个答案:

答案 0 :(得分:0)

简单的解决方案:您必须获取函数的递归调用的结果!

public function printDropdownTree($tree, $r = 0, $p = null) {
    $html = "";
    foreach ($tree as $i => $t) {
        $dash = ($t['parent'] == 0) ? '' : str_repeat('-', $r) . ' ';
        $html .= '<option value="'.$t['id'].'">'.$dash.''.$t['name'].'</option>';
        if ($t['parent'] == $p) {
            // reset $r
            $r = 0;
        }
        if (isset($t['_children'])) {
            $html .= $this->printDropdownTree($t['_children'], ++$r, $t['parent']);
        }
    }


     return $html;
}