php - Parse a multilevel array to view-file -
i need parse multilevel array view file.
my array this:
$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' ) ) this passed view file, this:
{test} {text} {/test} my problem is, show first level - want have unlimited levels.. possible without making workaround, create html in php-file , passes html view file?
it sounds need bit of recursion.
function recurse_output($input, $level = 0) { foreach($input $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"; } } when run against input, result is:
<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> it should pretty simple modify code not emit unless $key 'text'. don't know template system you've selected view mechanism, most of them permit similar recursive calling.
Comments
Post a Comment