title: How to merge table row with PHP array?
body: <p>I want to merge the table row if the date have an same id's. I have some array data like this :</p>
  
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              [id] =&gt; 2
              [date] =&gt; 05/13/2014
              [content] =&gt; some contents 1
              [act] =&gt; act1 act2 act3
          )
      [1] =&gt; Array
          (
              [id] =&gt; 2
              [date] =&gt; 05/28/2014
              [content] =&gt; some contents 1
              [act] =&gt; act1 act2 act3
          )
      [2] =&gt; Array
          (
              [id] =&gt; 7
              [date] =&gt; 06/04/2014
              [content] =&gt; some contents 2
              [act] =&gt; act1 act2 act3
          )
  )
  </code></pre>
  
  <p>Then I tried to group its data into the same id by :</p>
  
  <pre><code>            if($queryx = $this-&gt;mymodel-&gt;myfunction($par)){
                  $datax = $queryx;
              }           
              $i = 0;
              foreach ($datax  as $idx =&gt; $val) {
                  $dates[$val['id']][$i] = $val['date'].",".$val['id'];
                  $contn[$val['id']][$i] = $val['content'];
  
                  $i++;
              }
  </code></pre>
  
  <p>then :</p>
  
  <pre><code>            $j = 0; $y = 0;
              echo "&lt;table border='1'&gt;\r\n";
              foreach ($dates as $idx =&gt; $val) {
                  $tmpexp = explode(',', $val[$j]);
                  echo "&lt;tr&gt;\r\n";
                  echo "&lt;td rowspan='".count($val)."'&gt;".$tmpexp[0]."&lt;/td&gt;\r\n";
                  foreach ($val as $idx1 =&gt; $val1) {
                      if($idx1 &gt; 0)
                      {
                          echo "&lt;tr&gt;\r\n";
                      }
                      echo "&lt;td&gt;".$dates[$tmpexp[1]][$y]."&lt;/td&gt;\r\n";
                      if($idx1 &lt; 1)
                      {
                          echo "&lt;td rowspan='".count($val)."'&gt;".$contn[$tmpexp[1]][$y]."&lt;/td&gt;\r\n";                       
                          echo "&lt;td rowspan='".count($val)."'&gt;act1 act2 act3&lt;/td&gt;";
                          echo "&lt;/tr&gt;\r\n";
                      }
                      $y++;
                  }
                  $j++;   
              }
              echo "&lt;/table&gt;";
  </code></pre>
  
  <p>There is no problem if the data have at least two child, but if the date only have one child (see data with id's 7) there is show an error because Undefined offset. So how I add some handler if there is only one child on the data. There is my desired result:</p>
  
  <p>Please see this image:</p>
  
  <p><img src="https://i.stack.imgur.com/OGstj.png" alt=""></p>