在php上按组显示数组不能正常工作

时间:2015-08-31 04:18:42

标签: php arrays

我想要非常直接。

它是我用JavaScript触摸创建的HTML表单。它有一个增加/删除表格的选项(我没有在这里附加代码 - 只是想确保你知道这个以防万一。)。我在这个例子中我使用3行。

<tbody>
  <tr>
    <td><input class="case" type="checkbox"/></td>
    <td><input type="text" data-type="productCode" name="itemNo[]" id="itemNo_1" class="form-control autocomplete_txt" autocomplete="off" placeholder="Enter IMEI number"></td>
    <td><input type="text" data-type="productName" name="itemName[]" id="itemName_1" class="form-control autocomplete_txt" autocomplete="off" placeholder="Brand"></td>
    <td><input type="text" data-type="productName" name="brand[]" id="itemName_2" class="form-control autocomplete_txt" autocomplete="off" placeholder="Model"></td>
    <td><input type="number" name="price[]" id="price_1" class="form-control changesNo" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;"></td>
    <td><input type="number" value="1" name="quantity[]" id="quantity_1" class="form-control changesNo" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;" disabled></td>
    <td><input type="number" name="total[]" id="total_1" class="form-control totalLinePrice" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;" disabled></td>
 </tr>
</tbody>

一旦我通过php POST方法处理它,数据真的通过了。我把它们称为特定循环,如下所示。

$itemNo = $_POST['itemNo'];
$itemName = $_POST['itemName'];
$brand = $_POST['brand'];
$price = $_POST['price'];

foreach($itemNo as $itemNo) {
    echo $itemNo.'<br/>';
}

foreach( $itemName as $itemName ) {
print $itemName.'<br/>';
}

foreach( $brand as $brand ) {
print $brand.'<br/>';
}

foreach( $price as $price ) {
print $price.'<br/>';
}

它给我输出如下:

field 1 data of 1st row
field 1 data of 2nd row
field 1 data of 3rd row

field 2 data of 1st row
field 2 data of 2nd row
field 2 data of 3rd row

field 3 data of 1st row
field 3 data of 2nd row
field 3 data of 3rd row

field 4 data of 1st row
field 4 data of 2nd row
field 4 data of 3rd row

我实际上正在努力获得这样的输出 -

field 1 data of 1st row
field 2 data of 1st row
field 3 data of 1st row
field 4 data of 1st row

field 1 data of 2nd row
field 2 data of 2nd row
field 3 data of 2nd row
field 4 data of 2nd row

field 1 data of 3rd row
field 2 data of 3rd row
field 3 data of 3rd row
field 4 data of 3rd row

所以希望我可以使用单循环来获得所需的输出。

任何想法或帮助都应该受到赞赏。

由于 菲奥娜

1 个答案:

答案 0 :(得分:0)

您可以利用$key的{​​{1}}部分来获得所需的结果。见http://php.net/manual/en/control-structures.foreach.php

foreach($array as $key => $value)