Multiple input fields from PHP table To summarize from a previous question, the user selected a starting date, an ending date, and a region. From these choices, an array of weeks and regions are created, and users enter values ("points") that correspond to a particular week and region. This is what it looks like right now (some parts are hard-coded for testing purposes):

$date_array = Array("01/01/2012", "01/08/2012", "01/15/2012");
$region_array = Array("NYC", "DC");

&lt;form name="test_form" id="test_form" method="post"&gt;
&lt;table border="1"&gt;
    &lt;tr&gt;
        &lt;td&gt;&lt;/td&gt;
        &lt;?php
            foreach ($date_array as $date)
                {
                echo "&lt;td&gt;".$date."&lt;/td&gt;";
                }
        ?&gt;
    &lt;/tr&gt;
    &lt;?php
    foreach ($region_array as $region)
        {
        echo "&lt;tr&gt;";
        echo "&lt;td&gt;".$region."&lt;/td&gt;";
        foreach ($date_array as $date)
            {
            echo '&lt;td&gt;&lt;input type="text" name='.$region."-value1_".$date.'&gt;&lt;input type="text" name='.$region."-value2_".$date.'&gt;&lt;/td&gt;';

            }
        echo "&lt;/tr&gt;";
        }
    ?&gt;
&lt;/table&gt;
&lt;input type="submit" name="submit"&gt;




And the next part:

if (isset($_POST['submit']))
{
    foreach ($_POST as $k=&gt;$v)
    {
    if ($k != "submit") // use even and odd count to differentiate TRP and CPP
        {
        // split the form input on _
        $input = explode("_", $k);
        echo "&lt;BR&gt;Region: " . $input[0];
        echo "&lt;BR&gt;Date: " . $input[1];
        echo "&lt;BR&gt;Value: " . $v;
        }
    echo "&lt;br/&gt;";
    }
}


So basically, in this example it outputs 12 times:




Region: NYC-value1
Date: 01/01/2012
Value: 500

Region: NYC-value2
Date: 01/01/2012
Value: 5

Region: NYC-value1
Date: 01/08/2012
Value: 600



I want to catch the value1 and value2 while the loop is running, but as it stands it loops through both of them before going onto the next date/region. My idea, as I put in comments, was to have a counter of even and odd numbers and simply tell if it was doing value1 or value2 based on that.

Is this a good idea or am I missing something fundamentally simpler?
Try this:

&lt;?php
if(isset($_POST['submit'])){
    foreach ($_POST as $k=&gt;$v){
        // use even and odd count to differentiate TRP and CPP
        if ($k != "submit") {
            // split the form input on _
            $input = explode("_", $k);
            echo "&lt;BR&gt;Region: " . $input[0];
            echo "&lt;BR&gt;Date: " . $input[2];
            if($input[1] == "value1"){
                echo "&lt;BR&gt;Value 1: " . $v;
            }
            elseif($input[1] == "value2"){
                 echo "&lt;BR&gt;Value 2: " . $v;
            }
         }
        echo "&lt;br/&gt;";
     }
}
else {
    $date_array = Array("01/01/2012", "01/08/2012", "01/15/2012");
    $region_array = Array("NYC", "DC");
?&gt;
    &lt;form name="test_form" id="test_form" method="post" action=""&gt;
        &lt;table border="1"&gt;
            &lt;tr&gt;
                &lt;td&gt;&lt;/td&gt;
&lt;?php
                foreach ($date_array as $date){
                    echo "&lt;td&gt;".$date."&lt;/td&gt;";
                }
?&gt;
            &lt;/tr&gt;
&lt;?php
        foreach ($region_array as $region){
            echo "&lt;tr&gt;";
            echo "&lt;td&gt;".$region."&lt;/td&gt;";
            foreach ($date_array as $date){
                echo '&lt;td&gt;&lt;input type="text" name='.$region."_value1_".$date.'&gt;&lt;input type="text" name='.$region."_value2_".$date.'&gt;&lt;/td&gt;';
            }
            echo "&lt;/tr&gt;";
        }
?&gt;
        &lt;/table&gt;
    &lt;input type="submit" name="submit"&gt;
&lt;/form&gt;
&lt;?php
}
?&gt;