将多级数组解析为视图文件

我需要将多级数组解析为我的视图文件。 我的阵列可能是这样的:
$test = array(
    1 => array(
        10 => array('text' => 'test'),
        15 => array( 
            12 => array('text' => 'Test')
        ),
        'text' => 'Nr. 1'
    ),
    4 => array(
        14 => array('text' => 'Hello'),
        'text' => 'Nr. 4'
    )
)
这将传递给视图文件,可能如下所示:
{test}
    {text}
{/test}
我的问题是,这只会显示第一级 - 我希望有无限级别..这可能没有解决方法,我在PHP文件中创建HTML然后将HTML传递给视图文件?     
已邀请:
听起来你需要一点递归。
function recurse_output($input, $level = 0) {
    foreach($input as $key => $value) {
        echo "n", str_repeat(" ", $level);
        echo "<div>{$key} is: ";
        if(is_array($value))
            recurse_output($value, $level + 1);
        else
            echo $value;
        echo str_repeat(" ", $level);
        echo "</div>n";
    }
}
针对您的输入运行时,结果为:
<div>1 is: 
 <div>10 is: 
  <div>text is: test  </div>
 </div>

 <div>15 is: 
  <div>12 is: 
   <div>text is: Test   </div>
  </div>
 </div>

 <div>text is: Nr. 1 </div>
</div>

<div>4 is: 
 <div>14 is: 
  <div>text is: Hello  </div>
 </div>

 <div>text is: Nr. 4 </div>
</div>
除非
$key
'text'
,否则修改代码以便不发出任何内容应该非常简单。我不知道您为视图机制选择了哪个模板系统,但大多数模板系统允许类似的递归调用。     

要回复问题请先登录注册