Using a variable in a for loop: is it proper practice or is there a better approach The goal of this code was to create a table from an associative array.


I need to create horizontal rows where  the keys need to be used as "headers" while the values are posted below each header.  
I need to be able to reuse the code with associative arrays of unknown lengths and varying number of columns allowed in the tables.


I realize this is not set up as a function. I'm just playing with the code for now.

The code that I have actually does work. I'm not sure if it's "proper" form especially here - $startIndex = $index in the for loop.

I create two rows at a time - one for the header, one for the values. After the end of these two rows, I'm re-setting the index on the for loop so it runs through the next group in the array. I'm trying to get better at coding and would like some input on what I've done. 

    &lt;h2&gt;Print out a horizontal table with a capability to vary the number of  cells across&lt;/h2&gt;
&lt;table class="&lt;?php echo $tableClass; ?&gt;"&gt;
&lt;caption&gt;Horizontal&lt;/caption&gt;

&lt;?php 
$array = array(
    "A" =&gt; 1,
    "B" =&gt; 2,
    "C" =&gt; 3,
    "D" =&gt; 4,
    "E" =&gt; 5,   
    "F" =&gt; 6,    
    "G" =&gt; 7,    
    "H" =&gt; 8,
    "I" =&gt; 9,    
    "J" =&gt; 10,   
    "K" =&gt; 11,   
    "L" =&gt; 12,
    "M" =&gt; 13        
);

$maxColumns = 8;//set the number of columns allowed in the table
$arrayOfKeys = array_keys($array);//grab the keys from the initial array
$count = count($arrayOfKeys);//gets a count of the number of items in  the array
$maxRows = ceil($count/$maxColumns) * 2;//takes the count and determines the number of rows based on the number of items and the maxColumns * 2 for total rows  (need a "header" and value for each set of keys/values in the array
$endIndex = $maxColumns-1;//initialize the endIndex on the loop that runs the 
$startIndex = 0;//initialize the start index for the second loop which will loop through the array assigning keys to first row - value to second

for ($row=1; $row&lt;=$maxRows; $row++){//loops based on the number of rows required to generate the table for the number of cells required

    echo '&lt;tr&gt;';//start the row
    $rowEvenOdd = ($row % 2); //figures whether an even or odd row

    for ($index=$startIndex; $index&lt;=$endIndex; $index++){//creates the rows
            if($rowEvenOdd == 1) {//if the row is odd, grab and display the key
                if(array_key_exists($index, $arrayOfKeys))
                    {$thiskey = $arrayOfKeys[$index];}
                    else {$thiskey ="&amp;nbsp;";}
                echo "&lt;td&gt;$thiskey&lt;/td&gt;";
                }//end of odd rows
            if($rowEvenOdd == 0) {//if the row is even, grab and display the value
                if(array_key_exists($index, $arrayOfKeys))
                    {$thisvalue = $array[$arrayOfKeys[$index]];}
                    else {$thisvalue ="&amp;nbsp;";}            
                echo "&lt;td&gt;". $thisvalue ."&lt;/td&gt;";
                }//end of odd rows          
    }//end of loop to generate content for the cells
    echo '&lt;/tr&gt;';//end the row
    if($rowEvenOdd == 0) {$startIndex = $index; $endIndex = $index + $maxColumns-1;/* reset the index begin and end  to loop through next group of rows */                           }
    }//end loops that creates the rows

?&gt;
&lt;/table&gt;
Use array_chunk() to split $rows into rows. Each row will be double high $doubleRows, so store the values in a small multi-dimension array $tablerows before echoing the rows.

// One single row.
$rows = array(
    "A" =&gt; 1,
    "B" =&gt; 2,
    "C" =&gt; 3,
    "D" =&gt; 4,
    "E" =&gt; 5,
    "F" =&gt; 6,
    "G" =&gt; 7,
    "H" =&gt; 8,
    "I" =&gt; 9,
    "J" =&gt; 10,
    "K" =&gt; 11,
    "L" =&gt; 12,
    "M" =&gt; 13
);

// Split into 1 or more rows of 8 columns each.
$doubleRows = array_chunk( $rows, 8, true );

// Rows are now double high.
// Keys on the first row and values on the second.
foreach ( $doubleRows as $columns ) {

    // Reset rows.
    $tableRows = array();
    foreach ( $columns as $key =&gt; $value ) {
        $tableRows['keys'][] = "&lt;td&gt;$key&lt;/td&gt;";
        $tableRows['values'][]  = "&lt;td&gt;$value&lt;/td&gt;";
    }

    echo "&lt;tr&gt;", join( '', $tableRows['keys'] ), "&lt;/tr&gt;";
    echo "&lt;tr&gt;", join( '', $tableRows['values'] ), "&lt;/tr&gt;";
}


Style

You used a lot of end comments. I try to avoid them and I try to avoid obvious comments like, // Start the row. To me, the &lt;tr&gt; tag is pretty self explanatory, but if it is not to you, then I understand why you did it.

Wrap long comments and use proper punctuation and capitalization. Comments are for humans. PHP strips them out. If you are the only person who ever sees your code, write your comments for the (different) person you will be in six months. The future you may appreciate comments that look like sentences and paragraphs. 

Be consistent with your indentation and white space. Always indent the same number of characters. This foreach is indented 4 spaces (or 1 tab). The conditional statement is indented 12 spaces (or 3 tabs).

    for ($index=$startIndex; $index&lt;=$endIndex; $index++){//creates the rows
            if($rowEvenOdd == 1) {//if the row is odd, grab and display the key


I would write it like this:

    // Create table rows.
    for ( $index = $startIndex; $index &lt;= $endIndex; $index++ ) {
        if ( $rowEvenOdd == 1 ) {
            // Row is odd. Grab and display the key.


I added some white space so it wouldn't look all scrunch up. I find it easier to read like that. The comment after the conditional tells you what the condition is instead of what the conditional does.

If this is your style for writing one line conditionals, I guess it is fine, but it looks scrunch up and harder to read.

if(array_key_exists($index, $arrayOfKeys))
    {$thisvalue = $array[$arrayOfKeys[$index]];}
    else {$thisvalue ="&amp;nbsp;";}


I prefer this:

if ( array_key_exists( $index, $arrayOfKeys ) ) {
    $thisvalue = $array[$arrayOfKeys[$index]];
} else {
    $thisvalue ="&amp;nbsp;";
}


Or this:

if ( array_key_exists( $index, $arrayOfKeys ) )
    $thisvalue = $array[$arrayOfKeys[$index]];
else
    $thisvalue ="&amp;nbsp;";


I tend to not add white space near brackets [] and to add it in parenthesis ().

Except for $thisvalue and $thiskey all your variable name use camel case. $thisValue and $thisKey would be more consistent.