Using nested foreach()s, not working as I expected?
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 appreciate开发者_Go百科d!!
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 ) )
I'm not at a computer with PHP at the moment, can you try this?
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];
}
}
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.
精彩评论