-1

我认为我的解决方案是在多维数组中,但是如何...

如果我在 HTML 表单中有以下(或类似的)(例如简化)

<select name="Postage[]" id="Unique-ID">
    <option value="1">Postage Option 1</option>
    <option value="2">Postage Option 2</option>
    <option value="3">Postage Option 3</option>
</select>
<input name="PostagePrice[]" id="Price-Unique-ID" value="" />

<select name="Postage[]" id="Unique-ID">
    <option value="1">Other Postage Option 1</option>
    <option value="2">Other Postage Option 2</option>
    <option value="3">Other Postage Option 3</option>
</select>
<input name="PostagePrice[]" id="Price-Unique-ID" value="" />

<select name="Postage[]" id="Unique-ID">
    <option value="1">Another Postage Option 1</option>
    <option value="2">Another Postage Option 2</option>
    <option value="3">Another Postage Option 3</option>
</select>
<input name="PostagePrice[]" id="Price-Unique-ID" value="" />

我如何将它存储到 PHP 数组中(这样我以后可以将它添加到我的数据库中)到目前为止,我有以下内容,但显然还没有完成

if (isset($_POST['Postage'])) {
  if (is_array($_POST['Postage'])) {
    foreach($_POST['Postage'] as $PostateID=>$PostageOption){
      // this is where i am tottally stuck
      // need to assosiate postageID with a Postage Option and a PostagePrice
    }
  }
}

我很抱歉听起来很愚蠢,但我还没有多维数组的“Erika”时刻

我将不胜感激任何建议

4

3 回答 3

0

$_POST['Postage']不是多维数组。如果你 var_dump($_POST['Postage']); 你可以很容易地看到它。它只是您选择中所有选定索引的数组:

array(3) {
    [0]=>
    string(1) "1"
    [1]=>
    string(3) "1"
    [2]=>
    string(3) "1"
  }

这是我为测试输入的内容:

在此处输入图像描述

在这里,我使用 for 循环获取邮费和相关价格:

<?php
$n = count($_POST['Postage']);
for ($i = 0; $i < $n; ++$i)
{
    print $_POST['Postage'][$i] . " " . $_POST['PostagePrice'][$i] . "<br>";
}

印刷:

1 1
2 2
2 3
于 2013-07-19T16:09:39.053 回答
0
<?PHP
$final = array_combine($_POST['Postage'], $_POST['PostagePrice']);
?>

通过这种方式,您将在此数组中关联 Postage 和 PostagePrice。

于 2013-07-19T16:35:21.713 回答
0

考虑如下输入数组:

<?
//Let your inputs be Postage1, Price1, Postage2, Price2...
//Then your Received POST will be..
$_POST = Array(
            "Postage" => Array( 0 => 'Postage1', 1 => 'Postage2', 2 => 'Postage3'),
            "PostagePrice"=> Array( 0 => 'Price1', 2 => 'Price2', 3 => 'Price3')
            );
//Now, you can see that index 0 points to a price of POSTAGE and so on 1 and 2..

//Store corresponding values in an Array.
$store = Array();

foreach($_POST['Postage'] as $PostateID=>$PostageOption){
    $store[$PostageOption] = $_POST['PostagePrice'][$PostateID];
}

print_r($store);

?>
于 2013-07-19T16:12:14.083 回答