使用嵌套的foreach(),不按我的预期工作?

时间:2011-09-03 17:16:25

标签: php arrays foreach nested-loops

我的想法是,我有一个包含两个可编辑字段的表单,从中发布数据,放入两个多维数组,然后包含第三个多维数组。

接下来,我尝试使用foreach()函数获取所有数组的键和值,最终执行计算:$nu_macro[$qu_meal][$qu_row] = $qu_rowval*$nu_macroval;这意味着要为数量分配值表格中的特定膳食和行(因此字段名称格式为quantity[meal_no][row_no])乘以宏观营养素的基值(格式为$nutrition[ingredient][macro_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 />

为了尝试诊断问题我

  • var_dump()上发送$x(请参阅PHP中的评论),返回array(2) { [0]=> array(5) { [0]=> int(50) [1]=> int(55) [2]=> int(60) [3]=> int(65) [4]=> int(70) } [1]=> array(5) { [0]=> int(75) [1]=> int(80) [2]=> int(85) [3]=> int(90) [4]=> int(95) } }

  • 回复$nu_macroval,返回190,190,190,190,190,1.9,1.9,1.9,1.9,1.9,1.9,1.9,1.9,1.9,1.9,0.5,0.5,0.5,0.5,0.5,19.5,19.5,19.5,19.5...(每组$ nu_macroval 5套)。

我意识到这个问题可能是太多的循环,但是有没有什么方法可以让我按照我想要的方式工作而不使用嵌套循环函数?

我对$nu_macro[$qu_meal][$qu_row] = $qu_rowval*$nu_macroval;基本上calories[0][0] = 10 * 190的任何帮助,评论或答案都非常非常感谢!!

P.S。我确实意识到这是一个很长的问题,也许因此有点难以解释,所以如果你什么都不懂,请求澄清,我会尽量澄清我想要的东西。

更新1:

print_r()的{​​{1}}返回如下,

$ _ POST [ '成分']

$_POST[]

$ _ POST [ '量']

Array ( [0] => Array ( [0] => Avocado Hass [1] => Baking Potato [2] => Banana [3] => Basmati Rice(Raw) [4] => Beef Mince, Lean ) [1] => Array ( [0] => Beef Rump Steak [1] => Brown Rice(Raw) [2] => Casein [3] => Chicken Breast [4] => Cocoa Powder, Organic ) )

1 个答案:

答案 0 :(得分:1)

我目前不在使用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];
    }
}

它应循环遍历所有成分并创建一个可以像$calories["Avocado"][0][0]一样访问的卡路里数组。我认为您的问题源于无法访问索引数组的键。