how to multiply array in multi dimensional array in php

Learner

I want to multiply quantity value in multi dimensional array and also get the result based on the quantity

This is the array:

$meal_plan[$res['week']][$res2['day']][$res3['meal_plan_type']][$res3['quantity']][]=$res4;

This is the loop:

foreach ($meal_plan as $week => $dayArr) {
            foreach ($dayArr as $day => $meatTypeArr) {
                $tFat = $tCal = $tKJoules = $tCarb  = $tProt =0;  

                foreach ($meatTypeArr as $mealType => $meals) {
                     foreach ($meals as $mealQuantity => $mealq) {  //quantity loop
                    foreach ($mealq as $meal_id => $meal) {

                        $tFat += $meal['total_fat'];
                        $tCal += $meal['calories'] ;
                        $tKJoules += $meal['kilojoules'] ;
                        $tCarb += $meal['carbs'] ;
                        $tProt += $meal['protien'] ;    
                    }

                }
            }
}

This is my array, I want to multiply the value of cabs/ protein/ fat etc with the quantity array. How to do that ?

Array
(
    [1] => Array
        (
            [1] => Array
                (
                    [Breakfast] => Array
                        (
                            [3] => Array
                                (
                                    [0] => Array
                                        (
                                            [recepy_id] => 451
                                            [recepy_name] => Egg Omelette
                                            [meal_id] => 1
                                            [total_fat] => 17.7
                                            [calories] => 315
                                            [kilojoules] => 1310
                                            [carbs] => 18.6
                                            [protien] => 18
                                        )
                                )
                        )

                    [Lunch] => Array
                        (
                            [2] => Array
                                (
                                    [0] => Array
                                        (
                                            [recepy_id] => 1016
                                            [recepy_name] => Reduced Fat Milk
                                            [meal_id] => 4
                                            [total_fat] => 3
                                            [calories] => 127
                                            [kilojoules] => 530
                                            [carbs] => 15.25
                                            [protien] => 10
                                        )
                                )

                            [1] => Array
                                (
                                    [0] => Array
                                        (
                                            [recepy_id] => 639
                                            [recepy_name] => Custard - Egg
                                            [meal_id] => 4
                                            [total_fat] => 5.2
                                            [calories] => 82
                                            [kilojoules] => 344
                                            [carbs] => 1.7
                                            [protien] => 6
                                        )
                                )

                            [4] => Array
                                (
                                    [0] => Array
                                        (
                                            [recepy_id] => 1026
                                            [recepy_name] => Beef Waldorf Salad
                                            [meal_id] => 2
                                            [total_fat] => 11
                                            [calories] => 288
                                            [kilojoules] => 1202
                                            [carbs] => 28
                                            [protien] => 19
                                        )
                                )
                        )

                    [Snack] => Array
                        (
                            [1] => Array
                                (
                                    [0] => Array
                                        (
                                            [recepy_id] => 997
                                            [recepy_name] => Avocado -  60g
                                            [meal_id] => 4
                                            [total_fat] => 13
                                            [calories] => 125
                                            [kilojoules] => 522
                                            [carbs] => 0.3
                                            [protien] => 1
                                        )
                                )
                        )   
                )    
        )    
)
trincot

From your code it seems you want separate totals per day. So then you would do it like this:

foreach ($meal_plan as $week => $dayArr) {
    foreach ($dayArr as $day => $meatTypeArr) {
        $tFat = $tCal = $tKJoules = $tCarb  = $tProt = 0;  
        foreach ($meatTypeArr as $mealType => $meals) {
            foreach ($meals as $mealQuantity => $mealq) {  //quantity loop
                foreach ($mealq as $meal_id => $meal) {
                    // Multiply each with $mealQuantity:
                    $tFat += $mealQuantity * $meal['total_fat'];
                    $tCal += $mealQuantity * $meal['calories'] ;
                    $tKJoules += $mealQuantity * $meal['kilojoules'] ;
                    $tCarb += $mealQuantity * $meal['carbs'] ;
                    $tProt += $mealQuantity * $meal['protien'] ;    
                }
            }
        }
        // Add to result array (a set of totals per day)
        $result[$week][$day] = [
            "total_fat" => $tFat,
            "calories" => $tCal,
            "kilojoules" => $tKJoules,
            "carbs" => $tCarb,
            "protein" => $tProt
        ];
    }
}

See it run on eval.in.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert multi dimensional array to single dimensional array in php ?

How to parse a multi dimensional array to PHP

How to convert multi dimensional json to php array?

php - how to sum multi-dimensional array

PHP create multi dimensional array

Multi dimensional php array not working

modifying multi dimensional array in PHP

PHP Search Multi Dimensional Array

Sort multi dimensional array in php

Convert Multi dimensional array PHP

Multi dimensional associative array in php

PHP How to split multi dimensional array into single array?

php how to convert multi-dimensional array to single array

how to sort multi dimensional array in PHP using array value?

How to create a multi-dimensional array from an indexed array in PHP?

How to multiply two dimensional array by factor

PHP array unique on multi dimensional array/object

Split Multi Dimensional array into single array :PHP

How to search in multi dimensional array

How to search a multi-dimensional array by multiple values at once in PHP?

How to separate and assign data in a multi dimensional array via a loop - PHP

How do I trim '\n' in multi dimensional array in php

How to remove duplicate values from a multi-dimensional array in PHP

How to echo data from a Multi-Dimensional Array in PHP?

How to export a multi dimensional array to a specific .csv layout with fputcsv PHP

How to remove specific layer in multi-dimensional array in PHP?

How to initialize a multi-dimensional array in PHP presetting the keys

How to sort multi-dimensional array with bubble sort in PHP?

How to search for a needle in multi-dimensional array in php?