<?xml version="1.0" encoding="UTF-8"?>
<code>
  <code>&lt;?php

// Example Usage: array_to_tablerows(array('x', 'x', 'x', 'x', 'x', 'x'), 4);
// Returns: &lt;tr&gt;&lt;td&gt;X&lt;/td&gt;&lt;td&gt;X&lt;/td&gt;&lt;td&gt;X&lt;/td&gt;&lt;td&gt;X&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;X&lt;/td&gt;&lt;td&gt;X&lt;/td&gt;&lt;td colspan="2"&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;
// Table with 4 columns with 6 values from left to right.

function array_to_tablerows($array = array(), $columns = 2) {
	if (!is_array($array) || $columns &lt; 1) return false;
	$table = '&lt;tr&gt;';
	$i = 1;
	$array_length = count($array);
	foreach ($array as $v) {
		$remainder = $i % $columns;
		$table .= "&lt;td&gt;$v&lt;/td&gt;";
		if ($i == $array_length) {
			$table .= ($remainder ? ('&lt;td colspan="' . abs($columns - $remainder) . '"&gt;&amp;nbsp;&lt;/td&gt;') : '') . '&lt;/tr&gt;';
		}
		elseif ($remainder == 0) {
			$table .= '&lt;/tr&gt;&lt;tr&gt;';
		}
		++$i;
	}
	return $table;
}


?&gt;</code>
  <comment>Coverts an Array of Values to a multi-column table row.
A working demo is available at http://section31.us/study/dev/php/arrayvalues_to_table.php
Source for demo site is here http://section31.us/study/dev/php/arrayvalues_to_table.php?source

</comment>
  <created-at type="datetime">2008-10-25T06:23:17+00:00</created-at>
  <id type="integer">559</id>
  <language>PHP</language>
  <permalink>array-to-tablerows</permalink>
  <refactors-count type="integer">6</refactors-count>
  <title>Array To Tablerows</title>
  <trackback-url></trackback-url>
  <updated-at type="datetime">2008-10-28T07:57:15+00:00</updated-at>
  <user-id type="integer">1119</user-id>
  <refactors type="array">
    <refactor>
      <code>&lt;?php

// Example Usage: array_to_tablerows(array('x', 'x', 'x', 'x', 'x', 'x'), 4);
// Returns: &lt;tr&gt;&lt;td&gt;X&lt;/td&gt;&lt;td&gt;X&lt;/td&gt;&lt;td&gt;X&lt;/td&gt;&lt;td&gt;X&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;X&lt;/td&gt;&lt;td&gt;X&lt;/td&gt;&lt;td colspan="2"&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;
// Table with 4 columns with 6 values from left to right.

function array_to_tablerows($array = array(), $columns = 2) {

    if (!is_array($array) || $columns &lt; 1) return false;

    $table='';
    $arrayLen = count($array);
    for ($i=0; $i&lt;$arrayLen; $i+=$columns) {
         $table .= '&lt;tr&gt;&lt;td&gt;'. implode('&lt;/td&gt;&lt;td&gt;',array_slice($array, $i, $columns)) . '&lt;/td&gt;&lt;/tr&gt;';
    }
    
    $colspan = $columns - ($arrayLen % $columns);
    if ($colspan != $columns) {
         $table = substr($table,0,strlen($table)-5).'&lt;td colspan="'. $colspan .'"&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;';
    }
    return $table;
}

?&gt;</code>
      <code-id type="integer">559</code-id>
      <comment>Hi, try this version, it use the array_slice predefined function to simplify the code.

Edit: overlooked the &lt;td colspan="2"&gt;&amp;nbsp;&lt;/td&gt; part. Fixed.</comment>
      <created-at type="datetime">2008-10-25T07:00:10+00:00</created-at>
      <id type="integer">56159</id>
      <language>PHP</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Array To Tablerows</title>
      <user-id type="integer">337</user-id>
      <user-name>Eineki</user-name>
      <user-website>http://eineki.wordpress.com</user-website>
    </refactor>
    <refactor>
      <code>&lt;?php
function array_to_tablerows($ary, $max) { return array_create_table($ary, 0, $max); }
function array_create_table($ary, $idx, $max) {
	if(empty($ary)) {
		return $idx ? '&lt;td colspan="'.($max-$idx).'"&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;' : '&lt;/tr&gt;';
	} elseif(!$idx) $out.= '&lt;tr&gt;';
	$out .= "&lt;td&gt;".array_shift($ary)."&lt;/td&gt;";
	if(!($idx = ($idx + 1) % $max) &amp;&amp; !empty($ary)) $out.= '&lt;/tr&gt;';
	return $out . array_create_table($ary, $idx, $max);
}
?&gt;</code>
      <code-id type="integer">559</code-id>
      <comment>Here is the shortest I could get your code, it uses recursion, but there is a lot less arithmetic</comment>
      <created-at type="datetime">2008-10-26T21:03:48+00:00</created-at>
      <id type="integer">56259</id>
      <language>PHP</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Array To Tablerows</title>
      <user-id type="integer">1108</user-id>
      <user-name>halogenandtoast</user-name>
      <user-website>halogenandtoast.com</user-website>
    </refactor>
    <refactor>
      <code>&lt;?php
function array_to_tablerows($ary, $max) {
	return empty($ary) ? null : "&lt;tr&gt;".implode("&lt;/tr&gt;&lt;tr&gt;", array_map(create_function('$a', 'return "&lt;td&gt;".implode("&lt;/td&gt;&lt;td&gt;", $a)."&lt;/td&gt;".((count($a) % '.$max.' == 0) ? null : "&lt;td colspan=\"".('.$max.' - count($a))."\"&gt;&amp;nbsp;&lt;/td&gt;");'), array_chunk($ary, $max)))."&lt;/tr&gt;";
}

?&gt;</code>
      <code-id type="integer">559</code-id>
      <comment>Okay had to try and make a one-liner out of this, probably not as useful as my last solution but interesting none-the-less.</comment>
      <created-at type="datetime">2008-10-26T22:41:12+00:00</created-at>
      <id type="integer">56265</id>
      <language>PHP</language>
      <rating type="integer">3</rating>
      <ratings-count type="integer">2</ratings-count>
      <title>On Array To Tablerows</title>
      <user-id type="integer">1108</user-id>
      <user-name>halogenandtoast</user-name>
      <user-website>halogenandtoast.com</user-website>
    </refactor>
    <refactor>
      <code>&lt;?php
function array_to_tablerows($array = array(), $cols = 2) {
   if (!is_array($array) || $cols &lt; 1) return false;
   for ($i=0, $tbl='', $len = count($array), $i&lt;$len; $i+=$cols) 
         $tbl .= '&lt;tr&gt;&lt;td&gt;'. implode('&lt;/td&gt;&lt;td&gt;',array_slice($array, $i, $cols)) . '&lt;/td&gt;&lt;/tr&gt;';
   return ($len % $cols) ? substr($tbl,0,strlen($tbl)-5).'&lt;td colspan="'.($cols-($len % $cols)).'"&gt;&amp;nbsp&lt;/td&gt;&lt;/tr&gt;' : $tbl;
}
</code>
      <code-id type="integer">559</code-id>
      <comment>My previous version shortened is only 4 lines. I prefer the other one though</comment>
      <created-at type="datetime">2008-10-26T22:57:38+00:00</created-at>
      <id type="integer">56267</id>
      <language>PHP</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Array To Tablerows</title>
      <user-id type="integer">337</user-id>
      <user-name>Eineki</user-name>
      <user-website>http://eineki.wordpress.com</user-website>
    </refactor>
    <refactor>
      <code></code>
      <code-id type="integer">559</code-id>
      <comment>cool version the one liner one. missed the fifth star just for a mouse glitch</comment>
      <created-at type="datetime">2008-10-26T23:00:22+00:00</created-at>
      <id type="integer">56268</id>
      <language>PHP</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Array To Tablerows</title>
      <user-id type="integer">337</user-id>
      <user-name>Eineki</user-name>
      <user-website>http://eineki.wordpress.com</user-website>
    </refactor>
    <refactor>
      <code></code>
      <code-id type="integer">559</code-id>
      <comment>Love the one liner, but at the same time omg. </comment>
      <created-at type="datetime">2008-10-28T07:57:13+00:00</created-at>
      <id type="integer">56388</id>
      <language>PHP</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Array To Tablerows</title>
      <user-id type="integer">919</user-id>
      <user-name>Chris Dean</user-name>
      <user-website>http://www.coderchris.com</user-website>
    </refactor>
  </refactors>
</code>
