【发布时间】:2011-12-14 08:37:48
【问题描述】:
由于某种原因,我无法设置循环时间,因为我正在使用 jQuery 动态生成表单。我已经对该主题进行了一些研究,通常会使用foreach 循环所有有效字段,但我不知道如何做这些:
<form action="testing.php" method="post" >
<input type="text" name="product[1][name]" value="product1"/>
<input type="text" name="product[1][color][]" value="product1color1"/>
<input type="text" name="product[1][color][]" value="product1color2"/>
<input type="text" name="product[1][color][]" value="product1color3"/>
<input type="text" name="product[2][name]" value="product2"/>
<input type="text" name="product[2][color][]" value="product2color1"/>
<input type="text" name="product[3][name]" value="product3"/>
<input type="text" name="product[3][color][]" value="product3color1"/>
<input type="text" name="product[4][name]" value="product4"/>
<input type="text" name="product[4][color][]" value="product4color1"/>
<input type="submit" />
我的测试代码最终变成了这样,它不起作用.=(
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$product=$_POST['product'];
//store everything that start with product into array
foreach($product as $key){
//loop for product.1 product.2 and so on.....
//echo name of current product
echo $product[$key]['name'];
foreach($product[$key]['color'][] as $point){
echo $point;
}//loop for every single available color field
}//end of product loop
}// end of post request
?>
【问题讨论】:
-
用 foreach($key['color'] as $point) 更正了 foreach($product[$key]['color'][] as $point)
标签: php arrays dynamic multidimensional-array foreach