I am trying to use recursion to loop through a multidimensional object, displaying a key value pair if the value is not an object, or invoking the same function from within if the value is a nested object. Here is my code:
<?php
function display_fields($data,$html='')
{
foreach($data as $key => $value)
{
if (is_object($value)) $html .= display_fields($value,$html);
else $html .= '
<div class="row">
<div class="col-xs-3">'.$key.'</div>
<div class="col-xs-9">'.$value.'</div>
</div>';
}
return $html;
}
This function would then initially be invoked passing the full object though.
I realise there might be better ways to do this, but I am particularly trying to learn about recursion, and would appreciate the chance to find out what I've done wrong here.
EDIT: I forgot to mention, the undesired outcome I'm getting is that the same data are getting repeated many, many times. So an object with 20 total properties might produce thousands of lines of results.