arrays - PhP how to calculate moments with variables rows -
i have 45 - 50 array rowss deserialized json homecoming values in standard php array string format, curl info looks following:
array ( [0] => 0.00634000 [1] => 20.54182841 ) array ( [0] => 0.00637479 [1] => 31.21047749 ) array ( [0] => 0.00637480 [1] => 2.23450852 ) ....
var_dump of info var_dump
i need perform next math calculation against homecoming info (note incremental 1,2,3..)
((1 * 0.00634000 * 20.54182841) + (2 * 0.00637479 * 31.21047749) + (3 * 0.00637480 * 2.23450852)) / ((0.00634000 * 20.54182841) + (0.00637479 * 31.21047749) + (0.00637480 * 2.23450852)) ....
assuming next 3 array elements
0.1302351921194 + 0.3979204795969542 + 0.042733634739888 / 0.1302351921194 + 0.1989602397984771 + 0.014244544913296
then
0.5708893064562422 / 0.3434399768311731 array moments = 1.662268067112285 array length = 3
what proper way set in php if not know how many rows in array?
simply utilize foreach()
. example:
$var1 = 0; $var2 = 0; $count = 0; foreach($arr $k=>$v){ if(is_numeric($v[0]) && is_numeric($v[1])){ $var2 += $v[0] * $v[1]; $var1 += $var2 * ($k + 1); $count++; } } $result = $var1 / $var2; echo 'result: ' . $result . '<br /> count: ' . $count;
php arrays json angular-momentum
No comments:
Post a Comment