0

我有来自表单提交的以下数组:

 Array ( [id0] => id [gd0] => 50% [q0] => 1 [p0] => 10 [t0] => 10 [id1] => id [gd1] => 65%     [q1] => 2 [p1] => 20 [t1] => 40 [id2] => id [gd2] => 90% [q2] => 2 [p2] => 510 [t2] => 1020 )

我想通过以如下新模式存储相同的值来使其成为二维数组:

Array (array([id0] => id [gd0] => 50% [q0] => 1 [p0] => 10 [t0] => 10 ) , array([id1] => id [gd1] => 65% [q1] => 2 [p1] => 20 [t1] => 40 ) , array([id2] => id [gd2] => 90% [q2] => 2 [p2] => 510 [t2] => 1020))

因此,我试图在另一个数组的新维度中重新排列类似的信息。我尝试了一个 foreach 循环,但没有运气:

 $items = array();
                $X = -1; // the index of the first dimension
                $Y = 0; // the second index


 foreach ($_POST as $val) {


                    if ($val == 'id') {

                        $X++;
                        $Y = 0;
                    } else {

                        $items[$X][$Y] == $val;

                        // increment the second index to prevent overwriting

                        $Y++;
                    }
                }

                print_r($items);

但是,它不起作用。print_r()仅显示Array()

4

2 回答 2

5

您在语句中使用相等运算符==而不是赋值运算符。=else

于 2012-10-12T18:21:25.367 回答
0

我假设您在表单提交中有某种数组输入。如果是这样,您可以尝试像这样命名字段。它会直接在 $_POST 变量中为您提供一个二维数组。

<input type="text" name="data[0][id]" />
<input type="text" name="data[0][gd]" />
<input type="text" name="data[0][q]" />
<input type="text" name="data[0][p]" />
<input type="text" name="data[0][t]" />

<input type="text" name="data[1][id]" />
<input type="text" name="data[1][gd]" />
<input type="text" name="data[1][q]" />
<input type="text" name="data[1][p]" />
<input type="text" name="data[1][t]" />

<input type="text" name="data[2][id]" />
<input type="text" name="data[2][gd]" />
<input type="text" name="data[2][q]" />
<input type="text" name="data[2][p]" />
<input type="text" name="data[2][t]" />

$_POST 数组看起来像:

array(
  "data" => array(
    0 => array("id" => "value", "gd" => "value" ... ),
    1 => array("id" => "value", "gd" => "value" ... ),
    2 => array("id" => "value", "gd" => "value" ... ),
  )
)
于 2012-10-12T18:35:06.170 回答