PHP Recursive Multidimensional Array to HTML nested code - PHP - SitePoint Forums | Web Development & Design Community

PHOTO EMBED

Wed Dec 02 2020 05:06:57 GMT+0000 (Coordinated Universal Time)

Saved by @mvieira

function recursive($array, $level = 0)
{
    foreach($array as $key => $value)
	{
        
		if (!is_array($value) && $key == "tag") { echo str_repeat(" ", $level), "[".$value."]", ''; }
		
		//If $value is an array.
        if(is_array($value))
		{
			//We need to loop through it.
            recursive($value, $level + 1);
        } 
		else
		{
            //It is not an array, so print it out.			
			if ($key == "tag") { echo "[/".$value."]", '<br>'; }
						
        }
    }
}

$tree_2 = recursive($tree_1);

echo $tree_2;

content_copyCOPY

https://www.sitepoint.com/community/t/php-recursive-multidimensional-array-to-html-nested-code/256533