title: Display php nested array results continuously in two column table
body: <p>I have a nested array grouping three other arrays: $online, $busy and $offline, in order to display results in this exactly order.</p>
  
  <pre><code>$conteudo = array($online, $ocupado, $offline);
  </code></pre>
  
  <p>The desired result would be a table in two columns to display results in continuous flow like this:</p>
  
  <pre><code>online1 | online2
  online3 | busy1
  busy2   | offline1
  offline2| offline3
  offline4|
  </code></pre>
  
  <p>I've been trying lots of <code>foreach</code> loops and playing around changing the html tags, but the closest I can arrive of the desired result is this:</p>
  
  <pre><code>&lt;table width='100%' cellpadding='5' border="1"&gt;
  &lt;?php
  $i = 1; //contador de colunas
  $max_colunas = 2; // numero de colunas
  foreach ($conteudo as $row) {
      echo "&lt;tr&gt;";
      foreach ($row as $col) {
          foreach ($col as $cell) {
              if ($i == 1) {
                  echo "&lt;td&gt;" . $cell . "&lt;/td&gt;";
              } elseif ($i == $max_colunas){
                  echo "&lt;td&gt;" . $cell . "&lt;/td&gt;&lt;/tr&gt;";
                  $i = 0;
              } else {
                  echo "&lt;td&gt;" . $cell . "&lt;/td&gt;";
              }
              $i++;
          }
      }   
      echo "&lt;/tr&gt;"; 
  }   
  </code></pre>
  
  <p>This code will output a table like this:</p>
  
  <pre><code> onine1  | online2 |online3
   busy1   | busy2   |
   offline1|offline2 |offline3|offline4
  </code></pre>
  
  <p>I can't find out why it ignores completely <code>$max_colunas</code>, seems like it prints all the elements inside the array in row.</p>
  
  <p>If I remove lines:</p>
  
  <pre><code> echo "&lt;tr&gt;";
   echo "&lt;/tr&gt;";
  </code></pre>
  
  <p>from beginning and end of <code>foreach</code> it will output all in a row like this:</p>
  
  <pre><code>onine1  | online2 |online3 | busy1 | busy2 |offline1|offline2 |offline3|offline4
  </code></pre>
  
  <p>Any suggestions to get the desired output format will be so appreciated.</p>
  
  <hr>
  
  <p>Edited on 17/01:</p>
  
  <p>This is how I'm getting the arrays from:</p>
  
  <pre><code>//group people
  $online = array(); //group online
  $ocupado = array(); //group ocupado
  $offline = array(); //group offline
  
  //select people to group
  $atendentes = mysql_query("SELECT nome FROM atendentes ORDER BY nome") or die(mysql_error());
  $atendentedb = array();
  
  //put selected people in array
  while ($row = mysql_fetch_assoc($atendentes)) {
      $atendentedb[] = array('nome' =&gt; $row["nome"], 'online' =&gt; false);
  }
  
  
  //take people online now and check in selected people
  $names = modWhosonlineCustom::getOnlineUserNames();
  
  foreach ($names as $name):
      //foreach ($atendentedb as $atendente):
      for($i = 0; $i &lt; count($atendentedb); $i++):
          $att = strtolower($name-&gt;username);
  
          if ($atendentedb[$i]['nome'] == $att):          
              $atendentedb[$i]['online'] = true;
              break;
          endif;
  
     endfor;
  endforeach;
  
  //check each selected people
  foreach ($atendentedb as $atendente) :
      //save temporary data
      $online_ = $atendente['online'];
      $nome_   = $atendente['nome'];  
  
      //if selected people online
      if ($online_) :
          //take status to show
          $status = mysql_query("SELECT status FROM atendentes WHERE nome = '$nome_' LIMIT 1") or die(mysql_error());
          while ($row = mysql_fetch_assoc($status)):
              $statusdb = $row["status"];
          endwhile;
  
          //verify and save deppending on status
          switch ($statusdb):
  
              //if online
              case "disponivel":
                  $descricao = mysql_query("SELECT hp_online FROM atendentes WHERE nome = '$nome_' LIMIT 1") or die(mysql_error());
                  while ($row = mysql_fetch_assoc($descricao)):
                  $online[] = array('info'=&gt;$row['hp_online']);
                  endwhile;
                  break;
  
          //if busy
              case "ocupado":
                  $descricao = mysql_query("SELECT hp_busy FROM atendentes WHERE nome = '$nome_'  LIMIT 1") or die(mysql_error());
                  while ($row = mysql_fetch_assoc($descricao)):
                      $ocupado[] = array('info'=&gt;$row['hp_busy']);
                  endwhile;
                  break;
          endswitch;
  
      //if offline
      else:
          $descricao = mysql_query("SELECT hp_offline, horario FROM atendentes WHERE nome = '$nome_' LIMIT 1") or die(mysql_error());
          while ($row = mysql_fetch_assoc($descricao)):
             $offline[] = array('info'=&gt;$row['hp_offline'], 'horario'=&gt;$row['horario']);
          endwhile;
  
      endif;
  endforeach;
  </code></pre>
  
  <p><strong>EDITED</strong></p>
  
  <p>So after following help instructions from DaveRandom I got to this code, which is really a drop away from the right format, except for the "mysterious" behaviour with results coming from array <code>$offline</code>, that are displaying all in "block" (all cells in a row, or all cells in a column) while the other arrays are displaying perfectly(??).</p>
  
  <pre><code>   //group people
     $online = $ocupado = $offline = array();
  
     //select people to group
     $query = "SELECT nome, status, hp_online, hp_busy, hp_offline, horario
        FROM atendentes
        ORDER BY nome";
    $atendentes = mysql_query($query) or die(mysql_error());
    $atendentedb = array();
  
    // put selected people in array
    while ($row = mysql_fetch_assoc($atendentes)) {
    $atendentedb[strtolower($row['nome'])] = array_merge($row, array('online' =&gt; FALSE));
    }
  
    //take people online now and check in selected people
    $names = modWhosonlineCustom::getOnlineUserNames();
    foreach ($names as $name) {
    $uname = strtolower($name-&gt;username);
    if (isset($atendentedb[$uname])) $atendentedb[$uname]['online'] = TRUE;
    }
  
    //check each selected people
    foreach ($atendentedb as $name =&gt; $atendente) {
  
    //if selected people online
    if ($atendente['online']) {
  
   //verify and save deppending on status
   switch ($atendente['status']) {
  
    //if online
    case 'disponivel':
      $atendentedb[$name]['info'] = $online[] = $atendente['hp_online'];
      break;
  
    //if busy
    case 'ocupado':
      $atendentedb[$name]['info'] = $ocupado[] = $atendente['hp_busy'];
      break;
  
    }
  
    //if offline
    } else {
  
    $atendentedb[$name]['info'] = $offline[] = $atendente['hp_offline'];
  $atendentedb[$name]['info'] = $offline[] = $atendente['horario'];
  
    }
  
  }
  
    //*******Display Results
    $conteudo = array_merge($online, $ocupado, $offline);
    $max_colunas = 2; // numero de colunas
  
    // Start the table
    echo '&lt;table width="100%" cellpadding="5" border="1"&gt;'."\n";
  
    // Loop all the objects
    for ($i = 0, $j = 0; isset($conteudo[$i]); $i++) {
    if ($j == 0) {
    // Output the beginning of a row
    echo "  &lt;tr&gt;\n";
    }
    // Always output a data cell
    echo "    &lt;td&gt;$conteudo[$i]&lt;/td&gt;\n";
    if (++$j &gt;= $max_colunas) {
    // Output the end of a row and reset the cell counter
    echo "  &lt;/tr&gt;\n";
    $j = 0;
    }
    }
  
    if ($j) {
    // We may end up with an incomplete row at the end, so pad it with empty cells
    // and close the row
    while ($j++ &lt; $max_colunas) {
   echo "    &lt;td&gt;&lt;/td&gt;\n";
   }
   echo "  &lt;/tr&gt;\n";
   }
  
   // Close the table
   echo "&lt;/table&gt;";
  </code></pre>