1

下午好。

我正在尝试创建一个数组列表,其中一个键值为 ($_REQUEST['qty#']),其中“#”将是数组中项目的当前编号(因为它与收集此信息的表格)。

例如:

$itemdetails =  array(
    array(
    'qty' => ($_REQUEST['qty1']),
    'price' => 0.70,
    'pn' => 'TV-1000',

    array(
    'qty' => ($_REQUEST['qty2']),
    'price' => 0.99,
    'pn' => 'TV-5000'));

有什么方法可以自动确定 ($_REQUEST['qty']) 中的数字,而无需手动输入数字?

就是想。我的下一个猜测是将其全部输入数据库并从那里提取。

提前感谢一堆。

4

2 回答 2

0

What you should do is pass an array of data via POST/GET.

To do this in your inputs, you make the name value = qty[] for each input. Note the array syntax [] here.

PHP will automatically take all values for input with that array syntax and build an array out of it in $_POST/$_REQUEST.

So you would be able to access your array like

var_dump($_POST['qty']);
var_dump($_REQUEST['qty']);

That however still doesn't give the ability to match this to the price/pn as you need. So, let's take the array syntax one step further and actually put a key value in it like this:

<input name="qty[0]" ... />
<input name="qty[1]" ... />

By doing this you will be able to know exactly which array index matches which item (assuming you know the order the inputs were displayed in).

The would make $_POST['qty'][0] be the first item, $_POST['qty'][1] be the next and so on.

So assuming you also have you prices/pn in an array like this:

$item_array = array(
    0 => array('price' => 123, 'pn' = 'ABC'),
    1 => array('price' => 456, 'pn' = 'XYZ'),
    ...
);

You could then easily loop through the input quantities and build you final array like this:

$itemdetails = array();
foreach ($_REQUEST['qty'] as $key => $value) {
    $itemdetails[$key] = $item_array[$key];
    $itemdetails[$key]['qty'] = $value;
)

Also note, that if you are expecting this data to be passed via POST, it is considered best practice to use the $_POST superglobal rather than the $_REQUEST superglobal.

于 2013-04-05T18:11:25.670 回答
0

你需要循环...

$itemdetails = array(
        array(
                'price' => 0.70,
                'pn' => 'TV-1000'
        ),

        array(
                'price' => 0.99,
                'pn' => 'TV-5000'
        )
);

foreach ( $itemdetails as $k => &$item ) {
    $item['qty'] = $k + 1;
}
于 2013-04-05T18:03:27.870 回答