Search code examples
phparrayscodeignitermathoperation

codeigniter: how to perform mathematical operations using two arrays


i have two arrays like this

array 1

array(3) { [0]=> float(2.7742631687417) [1]=> float(2.5949809043991) [2]=> float(3.0174025996932) } 

array 2

array(3) { [0]=> float(2.7259998526469) [1]=> float(3.4826656582587) [2]=> float(3.2284968891602) } 

This array is dynamic , so the size of the array is not always the array ( 3 ) . sometimes array has a size of array ( 2 ) , array ( 4 ) , array ( 5 ) . The array just as an example .

I want to perform mathematical operations using the values ​​in the array using the looping function . The mathematical formula if I do it manually is like this

for example:

V1 = 2.7742631687417 / (2.7742631687417 + 2.7259998526469)

V2 = 2.5949809043991 / (2.5949809043991 + 3.4826656582587)

V3 = 3.0174025996932 / (3.0174025996932 + 3.2284968891602)

The numerical values ​​obtained from the existing values ​​in the array. "V" number depends on how many number of indexes that exist on the array . eg array ( 3 ) then "V" totaling 3 as well (V1, V2, V3).

so how I can perform mathematical operations and store it into the array as well ? I use PHP and CodeIgniter


Solution

  • Based on your description, it seems that you want to divide the value of array1 to the summation of the value of array1 and array2. The code would be:

     $arrlength = count($arr1);
        for($x = 0; $x < $arrlength; $x++) {
           $arr3[$x]=$arr1[$x]/($arr1[$x]+$arr2[$x]);
                }