Recode algorithm for merged cells on HTML table Is there any way to improve this algorithm maybe making it so faster avoid using foreach? Maybe with a recursion?
The output of my algorithm is a table with 3 merged columns, each row will have its own rowspan.  

$matrix=array("person1"=&gt;array("wifes"=&gt;array("diana"=&gt;array("born"=&gt;1982,"last_name"=&gt;"chapelle"),"carla"=&gt;array("born"=&gt;1983,"last_name"=&gt;"michael"),"serena"=&gt;array("born"=&gt;1984,"last_name"=&gt;"mickelson")
            ,"king"=&gt;array("born"=&gt;1980,"last_name"=&gt;"jordan")))
    ,"person2"=&gt;array("husbands"=&gt;array("charles"=&gt;array("born"=&gt;1983,"last_name"=&gt;"rambo")
    ,"minglen"=&gt;array("born"=&gt;1973,"last_name"=&gt;"rian"),"serrano"=&gt;array("born"=&gt;1914,"last_name"=&gt;"kingside"))));

$htmlx="&lt;table border='1'&gt;";
    $htmlx.="&lt;tr&gt;";
    $htmlx.="&lt;th&gt;object&lt;/th&gt;&lt;th&gt;type&lt;/th&gt;&lt;th&gt;names&lt;/th&gt;&lt;th&gt;detail_names&lt;/th&gt;&lt;th&gt;values_detail&lt;/th&gt;";
    $html_p=NULL; $p=0; 
    foreach ($matrix as $pkey =&gt; $pvalue) {
        $t=0;   $html_t=NULL;
        foreach ($pvalue as $tkey =&gt; $tvalue) {
            $n=0;   $html_n=NULL;   $rs_general=0;
            foreach ($tvalue as $nkey =&gt; $nvalue) {
                $d=0;   $html_d=NULL;   $rs_general+=count($nvalue);
                foreach ($nvalue as $dkey =&gt; $dvalue) {

                    $d++;
                    if($d==1){
                        $html_d="&lt;td&gt;".$dkey."&lt;/td&gt;&lt;td&gt;".$dvalue."&lt;/td&gt;";
                    }else{
                        $html_d.="&lt;tr&gt;&lt;td&gt;".$dkey."&lt;/td&gt;&lt;td&gt;".$dvalue."&lt;/td&gt;&lt;/tr&gt;";
                    }
                }
                $n++;
                if($n==1){
                    $html_n.="&lt;td rowspan='".count($nvalue)."'&gt;".$nkey."&lt;/td&gt;".$html_d;
                }else{
                    $html_n.="&lt;tr&gt;&lt;td rowspan='".count($nvalue)."'&gt;".$nkey."&lt;/td&gt;".$html_d."&lt;/tr&gt;";
                }
            }
            $t++;
            if($t==1){
                $html_t="&lt;td  rowspan='".$rs_general."'&gt;".$tkey."&lt;/td&gt;".$html_n;
            }else{
                $html_t.="&lt;tr&gt;&lt;td  rowspan='".$rs_general."'&gt;".$tkey."&lt;/td&gt;".$html_n."&lt;/tr&gt;";
            }
        }
        $p++;
        if($p==1){
            $html_p.="&lt;td rowspan='".$rs_general."'&gt;".$pkey."&lt;/td&gt;".$html_t;
        }else{
            $html_p.="&lt;tr&gt;&lt;td rowspan='".$rs_general."'&gt;".$pkey."&lt;/td&gt;".$html_t."&lt;/tr&gt;";
        }

    }
    $htmlx.="&lt;/tr&gt;".$html_p;
$htmlx.="&lt;/table&gt;";
echo $htmlx;  


I have been thinking about recursion function but I don’t get the solution yet, so any help will be welcome.
Some tips to make it faster 


Use references in foreach loops with &amp;, like foreach ($matrix as $pkey =&gt; &amp;$pvalue)
Don't use $htmlx variable, just output everything with echo
For some scecific purposes you may use ob_start(), ob_get_contents() and ob_end_clean() - it's much faster than concat like $html_d.=
Don't output html with php, just close php tags 

if($d==1){ ?&gt;
   &lt;td&gt;&lt;?php echo $dkey ?&gt;&lt;/td&gt;&lt;td&gt;&lt;?php echo $dvalue ?&gt;&lt;/td&gt;
&lt;?php }else{ ?&gt;