php将post数据排序为数组


php sort post data into arrays

我想从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[]" />

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

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
  )
)

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

您可以更容易地在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后变量中变成这样:

Array(
    Array [item1](
        [itemquantity] => 1
        [buyquantity] => 2
        [freeproduct] => 3
    )
    Array [item2](
        [itemquantity] => 4
        [buyquantity] => 5
        [freeproduct] => 6
    )
)
$result = array();
foreach ($_POST['itemquantity'] as $k => $v) {
    $result[] = array(
        'itemquantity' => $v,
        'buyquantity' => $_POST['buyquantity'][$k],
        'freeproduct' => $_POST['freeproduct'][$k],
    );
}

这应该可以完成任务(注意元素名称中的[][])。。

<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]" />

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

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

您可以使用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>";
?>