Php 使用嵌套的foreach()s,是否未按预期工作?
我的想法是,我有一个表单,有两个可编辑的字段,从中发布数据,放入两个多维数组,然后包括第三个多维数组 接下来,我尝试使用Php 使用嵌套的foreach()s,是否未按预期工作?,php,arrays,foreach,nested-loops,Php,Arrays,Foreach,Nested Loops,我的想法是,我有一个表单,有两个可编辑的字段,从中发布数据,放入两个多维数组,然后包括第三个多维数组 接下来,我尝试使用foreach()函数来获取所有数组的键和值,并最终执行计算:$nu_macro[$qu_mean][$qu_row]=$qu rowval*$nu_macroval是指将表格中特定膳食和行的数量值(因此字段名的格式为数量[膳食编号][行编号])乘以宏营养素的基本值(格式为$nutrient[配料][宏营养素]) 保留餐号和行号之所以重要,是因为创建的数组$nu_macro中的
foreach()
函数来获取所有数组的键和值,并最终执行计算:$nu_macro[$qu_mean][$qu_row]=$qu rowval*$nu_macroval
是指将表格中特定膳食和行的数量值(因此字段名的格式为数量[膳食编号][行编号]
)乘以宏营养素的基本值(格式为$nutrient[配料][宏营养素]
)
保留餐号和行号之所以重要,是因为创建的数组$nu_macro
中的值将被输出回具有相同餐号和行号的表单字段中
这是我的PHP:
include("nutrition.php");
$ingredient = $_POST['ingredient'];
$quantity = $_POST['quantity'];
foreach($ingredient as $in_meal => $in_mealval) {
foreach($in_mealval as $in_row => $in_rowval) {
foreach($quantity as $qu_meal => $qu_mealval) {
foreach($nutrition as $nu_ing => $nu_ingval) {
if($nu_ing == $in_rowval) {
foreach($nu_ingval as $nu_macro => $nu_macroval) {
foreach($qu_mealval as $qu_row => $qu_rowval) {
//echo $nu_macroval."<br />";
//$x[$qu_meal][$qu_row] = $qu_rowval*$nu_macroval;
}
}
}
}
}
}
}
$nutrition = array(
"Avocado Hass" => array(
"calories" => 190, "protein" => 1.9,
"carbohydrates" => 1.9,
"of_which_sugars" => 0.5,
"fats" => 19.5,
"s_fats" => 4.1,
"fibre" => 3.4,
"notes" => "0"),
"Baking Potato" => array(
"calories" => 140,
"protein" => 3.9,
"carbohydrates" => 30.7,
"of_which_sugars" => 1.2,
"fats" => 0.2,
"s_fats" => 0,
"fibre" => 2.7,
"notes" => "0"),
<input name="ingredient[0][0]" type="text" value="Avocado Hass" /><input name="quantity[0][0]" type="text" value="10" /><br />
<input name="ingredient[0][1]" type="text" value="Baking Potato" /><input name="quantity[0][1]" type="text" value="11" /><br />
<input name="ingredient[0][2]" type="text" value="Banana" /><input name="quantity[0][2]" type="text" value="12" /><br />
<input name="ingredient[0][3]" type="text" value="Basmati Rice(Raw)" /><input name="quantity[0][3]" type="text" value="13" /><br />
<input name="ingredient[0][4]" type="text" value="Beef Mince, Lean" /><input name="quantity[0][4]" type="text" value="14" /><br />
<input name="ingredient[1][0]" type="text" value="Beef Rump Steak" /><input name="quantity[1][0]" type="text" value="15" /><br />
<input name="ingredient[1][1]" type="text" value="Brown Rice(Raw)" /><input name="quantity[1][1]" type="text" value="16" /><br />
<input name="ingredient[1][2]" type="text" value="Casein" /><input name="quantity[1][2]" type="text" value="17" /><br />
<input name="ingredient[1][3]" type="text" value="Chicken Breast" /><input name="quantity[1][3]" type="text" value="18" /><br />
<input name="ingredient[1][4]" type="text" value="Cocoa Powder, Organic" /><input name="quantity[1][4]" type="text" value="19" /><br />
和我的表格:
include("nutrition.php");
$ingredient = $_POST['ingredient'];
$quantity = $_POST['quantity'];
foreach($ingredient as $in_meal => $in_mealval) {
foreach($in_mealval as $in_row => $in_rowval) {
foreach($quantity as $qu_meal => $qu_mealval) {
foreach($nutrition as $nu_ing => $nu_ingval) {
if($nu_ing == $in_rowval) {
foreach($nu_ingval as $nu_macro => $nu_macroval) {
foreach($qu_mealval as $qu_row => $qu_rowval) {
//echo $nu_macroval."<br />";
//$x[$qu_meal][$qu_row] = $qu_rowval*$nu_macroval;
}
}
}
}
}
}
}
$nutrition = array(
"Avocado Hass" => array(
"calories" => 190, "protein" => 1.9,
"carbohydrates" => 1.9,
"of_which_sugars" => 0.5,
"fats" => 19.5,
"s_fats" => 4.1,
"fibre" => 3.4,
"notes" => "0"),
"Baking Potato" => array(
"calories" => 140,
"protein" => 3.9,
"carbohydrates" => 30.7,
"of_which_sugars" => 1.2,
"fats" => 0.2,
"s_fats" => 0,
"fibre" => 2.7,
"notes" => "0"),
<input name="ingredient[0][0]" type="text" value="Avocado Hass" /><input name="quantity[0][0]" type="text" value="10" /><br />
<input name="ingredient[0][1]" type="text" value="Baking Potato" /><input name="quantity[0][1]" type="text" value="11" /><br />
<input name="ingredient[0][2]" type="text" value="Banana" /><input name="quantity[0][2]" type="text" value="12" /><br />
<input name="ingredient[0][3]" type="text" value="Basmati Rice(Raw)" /><input name="quantity[0][3]" type="text" value="13" /><br />
<input name="ingredient[0][4]" type="text" value="Beef Mince, Lean" /><input name="quantity[0][4]" type="text" value="14" /><br />
<input name="ingredient[1][0]" type="text" value="Beef Rump Steak" /><input name="quantity[1][0]" type="text" value="15" /><br />
<input name="ingredient[1][1]" type="text" value="Brown Rice(Raw)" /><input name="quantity[1][1]" type="text" value="16" /><br />
<input name="ingredient[1][2]" type="text" value="Casein" /><input name="quantity[1][2]" type="text" value="17" /><br />
<input name="ingredient[1][3]" type="text" value="Chicken Breast" /><input name="quantity[1][3]" type="text" value="18" /><br />
<input name="ingredient[1][4]" type="text" value="Cocoa Powder, Organic" /><input name="quantity[1][4]" type="text" value="19" /><br />
$\邮政['数量']
Array ( [0] => Array ( [0] => 10 [1] => 11 [2] => 12 [3] => 13 [4] => 14 ) [1] => Array ( [0] => 15 [1] => 16 [2] => 17 [3] => 18 [4] => 19 ) )
我现在不在使用PHP的电脑前,你能试试这个吗
for ($i=0; $i<count($ingredient); $i++) {
for ($j=0; $j<count($ingredient[$i]); $j++) {
$ingredientName = $ingredient[$i][$j];
$calories[$ingredientName][$i][$j] = $nutrition[$ingredientName]["calories"] * $quantity[$i][$j];
}
}
for($i=0;$i您能在POST请求中运行print\u r()并将其放在这里吗?还有什么是$x?$x
只是创建的一个随机数组(我想我会将其用于诊断,因为已经定义了$nu\u macro
)。我马上用print\r()更新:)。我认为使用关系数据库和/或一系列对象比使用多维数组更好。$nutrition
将存储在数据库中,其余部分完全根据输入进行计算。在创建表并将值放在表中之前,我只使用$nutrition
的数组来实现计算功能。@waitinforatrain:我已经用print\r()。现在让我试一试:)太棒了!刚刚将$carries[$ingredientName][$j]
更改为$carries[$i][$j]
以表示餐号(如$i
)。谢谢,所以,所以,非常感谢!刚刚看到我把它删掉了,编辑过。请记住,如果您看到嵌套的for/foreach循环超过3或4个,则说明某些操作出错。