0

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 returned 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) } }.

  • Echoed $nu_macroval which returns 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 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 ) )
Rick Ross
  • 23
  • 4
  • Can you run print_r() on your POST request and put it here? Also what is $x? – bcoughlan Sep 03 '11 at 17:28
  • `$x` is just a random array that's created (I thought i'd use it for diagnostics as `$nu_macro` is already defined). I'll update with print_r() in a second :). – Rick Ross Sep 03 '11 at 17:30
  • I'm thinking a relational database and/or a series of objects would be a better way to go instead of a multidimensional array. – Jared Farrish Sep 03 '11 at 17:31
  • `$nutrition` would be stored in a database, the rest are purely calculated on input. I'm only using an array for `$nutrition` to get the calculation functional before I create a table and place the values in there. – Rick Ross Sep 03 '11 at 17:35
  • @waitinforatrain: I've updated the OP with the `print_r()` returns. – Rick Ross Sep 03 '11 at 17:38
  • Was it intended that you set the quantity values all to 10,11,12,13... Is that just for testing purposes? – bcoughlan Sep 03 '11 at 17:40
  • Just for testing, the quantities could be any integer really. – Rick Ross Sep 03 '11 at 17:41
  • possible duplicate of [combinations: avoiding multiple nested foreach](http://stackoverflow.com/questions/1173556/combinations-avoiding-multiple-nested-foreach) – Nathan Osman Sep 03 '11 at 17:42
  • @George Edison: Not really, the OP in that post wants to clean up his already working code by the looks vote. I don't want to have to create some form of iterator class (I don't even know how in the first place) if I don't have to. – Rick Ross Sep 03 '11 at 17:46

1 Answers1

1

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.

bcoughlan
  • 25,987
  • 18
  • 90
  • 141
  • Yeah, that definitely seems like it should work. Let me give that a shot now :). – Rick Ross Sep 03 '11 at 17:51
  • 1
    Brilliant! Just changed `$calories[$ingredientName][$j]` to `$calories[$i][$j]` to represent the meal number (as `$i`). Thanks, so, so, so much! – Rick Ross Sep 03 '11 at 17:55
  • Just saw that I left that out, edited. Remember that if you ever see more than 3 or 4 for/foreach loops nested, something is being done wrong. – bcoughlan Sep 03 '11 at 17:57