动态生成多维数组的HTML表头

My requirement is to generate HTML table header with Dynamic multi dimensional array. e.g. Array will be like,

$Array = ('Sr.No.'=>'',
'Subject'=>array('Maths','Sci','History','Social Sci')
'Other Activities'=>array('Sports','Computer','Social'));

HTML Header should have Sr. No. header with rowspan=2. Subject and Other activities to have colspan=4 and colspan=3 respectively. It will look like following,

enter image description here

Array is dynamic i.e.it is possible that there will be sub headers like, Sci can have physics and chemistry.

If you look at the table which have generated. There is Sci header in second row. It is possible that there is one more row which will show two section under Sci i.e. Physics and Chemistry. I mean this is a dynamic array which could have 3 or 4 levels. I need a script which will work for nth level. It could be 3, 4 etc. I hope my question is clear now.

I am able to show data with recursive function. But generating Header with colspan and rowspan dynamically is where I am stuck.

<thead>
   <tr>
     <th rowspan=2>Sr.No.<th>
     <th colspan=4>Subject</th>
     <th colspan=3>Other Activities</th>
  </tr>
  <tr>
     <th>Maths</th>
     <th>Sci</th>
     <th>History</th>
     <th>Social Sci</th>
     <th>Sports</th>
     <th>Computer</th>
     <th>Social</th>
  </tr>
</thead>

Here you go:

$headers = "<thead>";
foreach ($Array as $key => $value)
{
    $headers .= "<th colspan='".max(sizeOf($Array[$key]),1)."'>".$key."</th>";
}
$headers .= "</thead>";

Here is a working DEMO

EDIT- BASED ON COMMENTS:
The demo above have the following code:

$Array = array("Sr.No." => "",'Subject'=>array('Maths','Sci','History','Social Sci'),  
              'Other Activities'=>array('Sports','Computer','Social'));

$table = "<table style='border-style: solid;'><thead>";
$headers = "<tr>";
$headers2 = $headers;
foreach ($Array as $key => $value)
{
  $headers .= "<th colspan='".max(sizeOf($Array[$key]),1)."' style='border-right: 1px solid;'>".$key."</th>";
 if(is_array($Array[$key]))
 {
     foreach ($Array[$key] as $key2 => $value2)
     {
         $headers2 .= "<th style='border: 1px dotted;'>".$value2."</th>";
     }
 }
 else{$headers2 .= "<th style='border:1px dotted;'></th>";}
}
$headers .= "</tr>";
$headers2 .= "</tr>";
$table .= $headers.$headers2."</thead></table>";

echo $table;  

Here are the results:

enter image description here