我正在为某些产品开发一个 PHP 订购页面。用户需要能够选择多种类型的产品,因此 HTML 表单中需要复选框。
我已经通过“名称”属性为复选框建立了一个数组。
我的问题是,我想在确认页面上显示用户选择的内容,所以我希望这些复选框的“值”最终返回产品名称和产品价格。据我所知,我只会坚持一个值,所以我试图解决这个问题:
<?php
//variables for the array and the count
$parray = $_POST['product'];
$pcount = count($parray);
//arrays for each product's information; i've only listed one for simplicity
$br = array("Big Red", 575);
/*The idea for this was for each product ordered to add a line giving the product information and display it. When the $key variable is assigned, it is stored simply as a string, and not an array (yielding [0] as the first letter of the string and [1] as the second), which I expected, but my general idea is to somehow use $key to reference the above product information array, so that it can be displayed here*/
if (!empty($parray))
{
for ($i=0; $i < $pcount; $i++)
{
$key = $parray[i];
echo "<tr><td height='40'></td><td>" . $key[0] . "</td><td>" . $key[1] . "</td></tr>";
}
}
?>
有没有办法让我的 $key 变量实际上就像它设置的数组名称一样?如果没有,有什么好的方法可以做到这一点吗?