Calculate with multiple arrays

Hi all,

Here is the context :
I’ve a post form in ajax which send fields in array for each one (example, field name = name[] and I can retrieve it in my php file called by ajax and display name1,name2 or the typical array.

Then I’ve several arrays, I merged all these in one and here is the result :
[php]Array ( [0] => Array ( [0] => field1_var1[1] => field1_var2[2] => field1_var3) [1] => Array ( [0] => field2_var1[1] => field2_var2[2] => field2_var3) [2] => Array ( [0] => field3_var1[1] => field3_var2[2] => field3_var3) )[/php]

I want to calculate for each variable of field1 (variable of field2 / (sum of variables field 3))
example : field1_var1 = field2_var1 / (sum field3), field1_var2 = field2_var2/ (sum field3)

If you have recommendation on how to proceed because I’m stuck about the logic,

Thanks by advance for your help

field1_var1 in way of variable naming sucks.

You would parse the array into a manageable piece, loop thru and do the calculations.

Thanks for your reply

This example is just to show you that the first array includes all the variables from the first field of my form, the second array the second field, etc.
So the output of the merged arrays is like that :
[php]
Array ( [0] => Array ( [0] => john[1] => paul[2] => pedro)
[1] => Array ( [0] => 22[1] => 11[2] => 90)
[2] => Array ( [0] => 50[1] => 100[2] => 21) )
[/php]

I tried to make a loop with a foreach execution but I haven’t the logic to obtain :
foreach element of array 0

  • element : element of array 1/ sum of array 2

in my example :
john : 22 / 171
then
paul : 11 / 171

For all my tests, I obtain :
john : 22 11 90 / 171

Thanks

Have you tried to loop thru the values?

Here is an example of what I did, and of course not working :
[php]
foreach ($result[0] as $value) {

foreach ($result[2] as $inside) {

   $myValue = $value . ($inside/ array_sum($result[2]));
print_r($myValue);

}}
[/php]

I’m not sure to call the second foreach at the good moment, for sure because the output is wrong…

If you have any advices.
Thanks

Sponsor our Newsletter | Privacy Policy | Terms of Service