php将数据发布到数组中

时间:2016-04-13 13:24:21

标签: php arrays post

我想从post获取一些输入值并将它们存储在一个数组中。以下是我的输入元素,它们是可重复的字段:

<input type="text" class="form-control" id="exampleInputPassword1" name="itemquantity[]" />
<input type="text" class="form-control gettitles" id="exampleInputPassword1" name="buyproduct[]" />
<input type="text" class="form-control gettitles" id="exampleInputPassword1" name="freeproduct[]" />

当我提交表单和print_r时,我得到以下内容(字段已重复):

Array ( [itemquantity] => Array ( 
                               [0] => 1 
                               [1] => 4 
                               ) 
        [buyproduct] => Array ( 
                               [0] => 2 
                               [1] => 5 
                              ) 
        [freeproduct] => Array ( 
                                [0] => 3 
                                [1] => 6 
                               )

我如何按重复提交的方式对它们进行分组?

所以例如我希望输出像这样:

Array(
  Array [0](
    [itemquantity] => 1
    [buyquantity] => 2
    [freeproduct] => 3
  )
  Array [1](
    [itemquantity] => 4
    [buyquantity] => 5
    [freeproduct] => 6
  )
)

任何帮助将不胜感激,谢谢!

5 个答案:

答案 0 :(得分:5)

您可以在PHP中更轻松地使用它:

<input type="text" class="form-control" id="exampleInputPassword1" name="item1[itemquantity]" />
<input type="text" class="form-control gettitles" id="exampleInputPassword1" name="item1[buyproduct]" />
<input type="text" class="form-control gettitles" id="exampleInputPassword1" name="item1[freeproduct]" />

<input type="text" class="form-control" id="exampleInputPassword1" name="item2[itemquantity]" />
<input type="text" class="form-control gettitles" id="exampleInputPassword1" name="item2[buyproduct]" />
<input type="text" class="form-control gettitles" id="exampleInputPassword1" name="item2[freeproduct]" />

这将成为PHP post变量:

Array(
    Array [item1](
        [itemquantity] => 1
        [buyquantity] => 2
        [freeproduct] => 3
    )
    Array [item2](
        [itemquantity] => 4
        [buyquantity] => 5
        [freeproduct] => 6
    )
)

答案 1 :(得分:1)

$result = array();
foreach ($_POST['itemquantity'] as $k => $v) {
    $result[] = array(
        'itemquantity' => $v,
        'buyquantity' => $_POST['buyquantity'][$k],
        'freeproduct' => $_POST['freeproduct'][$k],
    );
}

答案 2 :(得分:0)

这应该可以解决问题(注意元素名称中的[] [])..

<input type="text" class="form-control" id="exampleInputPassword1" name="[][itemquantity]" />
<input type="text" class="form-control gettitles" id="exampleInputPassword1" name="[][buyproduct]" />
<input type="text" class="form-control gettitles" id="exampleInputPassword1" name="[][freeproduct]" />

答案 3 :(得分:0)

您可以获取现有的$_POST数据并将其重新排列为合适的结构。循环遍历$_POST数据并填充具有所需输出的新数组,因此;

$output = array(); $i = 0;
foreach($_POST['itemquantity'] as $v) {
  $output[] = array(
    'itemquantity' => $v,
    'buyquantity' => $_POST['buyquantity'][$i],
    'freeproduct' => $_POST['freeproduct'][$i]
  );
  $i++;
}

答案 4 :(得分:0)

你可以使用for或foreach循环。你可以做这样的事情,

<?php
$a1 = array(1,2,3);
$a2 = array(4,5,6);
$new_array[] = array();
for($i=0; $i<count($a1); $i++){
    $new_array[$i]['a1'] = $a1[$i];
    $new_array[$i]['a2'] = $a2[$i];
}
echo "<pre>";
print_r($new_array);
echo "</pre>";
?>