使用嵌套的 foreach(),没有按我的预期工作?
我的想法是,我有一个带有两个可编辑字段的表单,从它们发布数据,放入两个多维数组中,然后包含第三个多维数组。
接下来,我尝试使用 foreach() 函数获取所有数组的键和值,最终执行计算:$nu_macro[$qu_meal][$qu_row] = $qu_rowval *$nu_macroval; 旨在分配表格中特定餐食和行的数量值(因此字段名称的格式为数量[meal_no][row_no]
)乘以大量营养素的基值(格式为$nutrition[ingredient][macro_ Nutrition]
)。
保留膳食的原因&行号很重要,因为创建的数组 $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.php:
$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 />
为了尝试诊断问题,我;
在
$x
上运行var_dump()
(请参阅 PHP 中的注释),返回array(2) { [0]=> ;数组(5) { [0]=>; int(50) [1]=> int(55)[2]==> int(60) [3]==> int(65)[4]==> int(70) } [1]=>;数组(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...
(每组5套$nu_macroval)。
我意识到问题可能是循环太多,但是有什么方法可以让它按照我想要的方式工作而不使用嵌套循环函数?
关于如何使 $nu_macro[$qu_meal][$qu_row] = $qu_rowval*$nu_macroval;
基本上达到 calories[0][0] = 10 的任何帮助、评论或答案* 190
将非常非常非常非常感谢!
PS我确实意识到这是一个很长的问题,也许因此有点难以解释,所以如果您不明白任何内容,请要求澄清,我会尽力澄清我想要的具体内容。
更新1:
$_POST[]的print_r()
返回如下,
$_POST['ingredients']
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 ) )
$_POST['quantity ']
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 ) )
The idea is that I have a form with two editable fields, data is posted from them, put into two multi-dimensional arrays, then a third multi-dimensional array is included.
Next, I have tried to use the foreach()
function to get the keys and values for all arrays, ultimately performing the calculation: $nu_macro[$qu_meal][$qu_row] = $qu_rowval*$nu_macroval;
which is meant to be assigned the value of the quantity for a specific meal and row in the form (hence why field name is in the format quantity[meal_no][row_no]
) multiplied by the base value of the macro nutrient (in the format $nutrition[ingredient][macro_nutrient]
).
The reason why keeping the meal & row numbers is important is because the values within the created array $nu_macro
will be output back into form fields with the same meal & row numbers.
Here's my 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;
}
}
}
}
}
}
}
And a few lines of nutrition.php:
$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"),
And my form:
<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 />
In order to attempt to diagnose the issue I;
Ran
var_dump()
on$x
(see comments in PHP) which returnedarray(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) } }
.Echoed
$nu_macroval
which returns190,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...
(5 sets of each $nu_macroval).
I realise that the issue is probably far too many loops, but is there any way I can get this to work the way that I want without using nested loop functions?
Any help, comments or answers on how I can get $nu_macro[$qu_meal][$qu_row] = $qu_rowval*$nu_macroval;
to essentially be calories[0][0] = 10 * 190
would be very very very very very much appreciated!!
P.S. I do realise this is a very long question, and perhaps as such it's a bit difficult to explain, so if you don't understand anything, please ask for clarification and I'll try to clarify what I want as specifically as I can.
UPDATE 1:
The print_r()
returns for $_POST[]
are as follows,
$_POST['ingredients']
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 ) )
$_POST['quantity']
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 ) )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我现在电脑上没有 PHP,你能试试这个吗?
它应该循环遍历所有成分并创建一个可以像
$calories["Avocado"][0][0]
一样访问的卡路里数组。我认为您的问题源于无法访问索引数组的键。I'm not at a computer with PHP at the moment, can you try this?
It should loop through all of the ingredients and create a calories array that can be accessed like
$calories["Avocado"][0][0]
. I think your problem is stemming from not being able to access the keys of your indexed arrays.