PHP - 基于键中的字符分离数组

I have a PHP array from a submitted form that looks like the following

$form_data = Array (
    [input_1_1] => Product Name
    [input_1_2] => $8.00
    [input_1_3] => 2
    [input_2_1] => Other Product Name
    [input_2_2] => $3.50
    [input_2_3] => 8
)

I am looking for the best way to parse this so I can build something markup like this:

<table>
    <tr>
        <td>Product Name</td>
        <td>$8.00</td>
        <td>2</td>
    </tr>
    <tr>
        <td>Other Product Name</td>
        <td>$3.50</td>
        <td>8</td>
    </tr>
</table>

What would be most kosher way to do this?

Should I split this array into smaller arrays based on the 1st number (and then loop them)? Alternatively should I just build the markup based on sets of three, like:

<table>
    <tr>
        <td><?php echo $form_data[1]; ?></td>
        <td><?php echo $form_data[2]; ?></td>
        <td><?php echo $form_data[3]; ?></td>
    </tr>
    <tr>
        <td><?php echo $form_data[4]; ?></td>
        <td><?php echo $form_data[5]; ?></td>
        <td><?php echo $form_data[6]; ?></td>
    </tr>
</table>

Any help/advice appreciated.

$last_group = '';
foreach ($form_data as $key => $value) {
    preg_match('/input_(\d+)/', $key, $match);
    if ($match[1] != $last_group) {
        if ($last_group) {
            echo "</tr>
";
        }
        echo "<tr>
";
        $last_group = $match[1];
    }
    echo "<td>$value</td>
";
}
if ($last_group) {
    echo "</tr>
";
}

It would be better to structure your form like this, though:

<input type="text" name="name[]">
<input type="text" name="price[]">
<input type="text" name="quantity[]">

Then the $_POST['name'], $_POST['price'] and $_POST['quantity'] will be arrays, and you can iterate over them:

foreach ($_POST['name'] AS $i => $name) {
    $price = $_POST['price'][$i];
    $quantity = $_POST['quantity'][$i];
    // Output the table row
}

take a look on this example: http://codepad.org/zhKbc4p1

$arr = array('input_1_1'=>'Name', 'input_1_2'=>'$8', 'input_1_3'=>1, 'input_2_1'=>'Name', 'input_2_2'=>'$28', 'input_2_3'=>1);
$chunk = array_chunk($arr, 3);

print_r($chunk);
foreach( $chunk as $val ){
    foreach($val as $v){
        echo $v . "
";
     }
    echo "

";
}

Expected HTML format:

foreach( $chunk as $val ){
    echo "<tr>";
    foreach($val as $v){
        echo "<td>" . $v . "</td>";
     }
    echo "</tr>";
}

Try This

$form_data = Array (
'[input_1_1]' => 'Product Name',
'[input_1_2]' => '$8.00',
'[input_1_3]' => '2',
'[input_2_1]' => 'Other Product Name',
'[input_2_2]' => '$3.50',
'[input_2_3]' => '8'
);

echo "&lt;table&gt;";
foreach($form_data as $k=>$v)
{
echo "&lt;tr&gt;";
echo "&lt;td&gt;".$v."&lt/td&gt;";
echo "&lt;/tr&gt;";
}
echo "&lt;/table&gt;";

Assuming each table row has 3 elements each;

<?php

echo "<table>
        <tr>";

$form_data = Array (
    "input_1_1" => "Product Name",
    "input_1_2" => "$8.00",
    "input_1_3" => "2",
    "input_2_1" => "Other Product Name",
    "input_2_2" => "$3.50",
    "input_2_3" => "8"
);

$intRowBefore = 1;
foreach( $form_data as $strKey => $strVal ) {

   $intRow = explode("_", $strKey)[1];

   if( $intRowBefore != $intRow ) {
      echo "</tr><tr>";
      $intRowBefore = $intRow;
   }

   echo "<td>". $strVal ."</td>";


}

echo "</tr></table>";

Consider

<?php
$form_data = array();
$form_data['input_1_1'] = 'Product Name';
$form_data['input_1_2'] = '$8.00';
$form_data['input_1_3'] = '2';
$form_data['input_2_1'] = 'Other Product Name';
$form_data['input_2_2'] = '$3.50';
$form_data['input_2_3'] = '8';

// Get unique keys
function get_unique_keys($keys)
{
    $arr = array();
    foreach($keys as $k=>$v){
        $arr[] = substr($k, 0, strlen($k)-2);
    }
    return array_unique($arr);
}

$keys = get_unique_keys($form_data);

// Output
printf("<table>
");
foreach($keys as $k)
{
    printf("  <tr>
");
    printf("    <td>%s</td>
", $form_data[$k.'_1']);
    printf("    <td>%s</td>
", $form_data[$k.'_2']);
    printf("    <td>%s</td>
", $form_data[$k.'_2']);
    printf("  </td>
");
}
printf("</table>
");

Which outputs:

<table>
  <tr>
    <td>Product Name</td>
    <td>$8.00</td>
    <td>$8.00</td>
  </td>
  <tr>
    <td>Other Product Name</td>
    <td>$3.50</td>
    <td>$3.50</td>
  </td>
</table>

See it in action: http://ideone.com/355STr


It's not super complicated. It gets the key "roots" we'll call them (input_1, input_2, etc) and loops over those, appending the additional _1,_2,_3 when needed.

A better solution long-term would be to redesign the form, but that's not always a possibility.

<?php
$form_data = array(
    'input_1_1' => 'Product Name',
    'input_1_2' => 8.00,
    'input_1_3' => 2,
    'input_2_1' => 'Other Product Name',
    'input_2_2' => 3.50,
    'input_2_3' => 8
);
end($form_data);
$counter = explode('_',key($form_data));
?>

<table>
    <?php
    $i = 1;
    $j = 1;
    for ($k = 1; $k <= $counter[1]; $k++) {
        ?>
        <tr>
            <td><?php echo $form_data['input_' . $j . '_' . $i]; ?></td>
            <td><?php $i++;echo $form_data['input_' . $j . '_' . $i]; ?></td>
            <td><?php $i++;echo $form_data['input_' . $j . '_' . $i]; ?></td>
        </tr>
        <?php
        if ($i == 3) {
            $i = 1;
            $j++;
        }
    }
    ?>
</table>