php - recursive function inside for loop -
i m trying create html in controller instead of js. there array unknown depth of arrays.
$tree = $repo->childrenhierarchy();
and function reads array , returns string of html values array elements.
public function recursive($tree) { $html = ""; foreach ($tree $t) { $html = $html . '<li> <span><i class="fa fa-lg fa-minus-circle"></i>' . $t['title'] . '</span>'; if ($t['__children'] != null) { $html = $html . '<ul>'; $this->recursive($t['__children']); $html = $html . '</ul>'; } else { $html = $html . '</li>'; } return $html; }
my problem cant hold total string because everytime function calls var html initialised, need hold string global cant figure how.
there shouldnt wrong storing value in class property while action ?
public $html = ""; public function recursive($tree) { foreach ($tree $t) { $this->html = $this->html . '<li> <span><i class="fa fa-lg fa-minus-circle"></i>' . $t['title'] . '</span>'; if ($t['__children'] != null) { $this->html = $this->html . '<ul>'; $this->recursive($t['__children']); $this->html = $this->html . '</ul>'; } else { $this->html = $this->html . '</li>'; } return $this->html; }
Comments
Post a Comment