unemployment Posted March 30, 2012 Share Posted March 30, 2012 I have a really confusing question. I dynamically generate arrays, but for the simplicity of the example we can just use two arrays. Array 1 ( [0] = 1 [1] = 5 ) Array 2 ( [0] = 2 [1] = 6 ) How can I make the arrays multiple based on the same key value? array 1[0] * array 2[0] = 2 array 1[1] * array 2[1] = 30 I then want to sum the total of the multiplication. $total = 2 + 30; I need to do this to calculate covariance for a statistics procedure I am trying to run but I can't wrap my head around how to do the logic. Quote Link to comment Share on other sites More sharing options...
Jessica Posted March 30, 2012 Share Posted March 30, 2012 1. Post your code in code tags to fix the formatting issue, and make it easy to read. 2. you'll want to do a foreach, with a value defined before the loop to hold the sum. Do you need the individual multiplication results or just the end total? Quote Link to comment Share on other sites More sharing options...
Jessica Posted March 30, 2012 Share Posted March 30, 2012 <?php $total = 0; foreach{$array1 AS $k=>$v){ $total += $v*$array2[$k]; } echo $total; ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 30, 2012 Share Posted March 30, 2012 <?php $total = 0; foreach{$array1 AS $k=>$v){ $total += $v*$array2[$k]; } echo $total; ?> The OP stated that he/she was only providing two as an example. That could be edited for multiple arrays, but not if the number is dynamic. Plus, if there are keys in array 2 that don't appear in array 1, they would be missed (although that may not be an issue). @op: You are getting these values from somewhere (a DB perhaps?). There is a very good chance you can do what you need when you are extracting the records and are making this more difficult than you need to. But, with what you have stated I have another approach that should work no matter how many arrays you have or which keys each array does or does not have $arry1 = array(1, 5); $arry2 = array(2, 6); //Put all data arrays into parent array $arraysList = array($arry1, $arry2); //Put values into new combined mufti-dimensional array //With the values in sub-arrays by common keys $masterArray = array(); foreach($arraysList as $array) { foreach($array as $key => $value) { $masterArray[$key][] = $value; } } //Calculate product of each value set and add to total $total = 0; foreach($masterArray as &$valueSet) { $total += array_product($valueSet); } echo $total; Quote Link to comment Share on other sites More sharing options...
unemployment Posted March 30, 2012 Author Share Posted March 30, 2012 <?php $total = 0; foreach{$array1 AS $k=>$v){ $total += $v*$array2[$k]; } echo $total; ?> That worked out great. I just needed to grab the total. Thanks! Quote Link to comment Share on other sites More sharing options...
unemployment Posted March 30, 2012 Author Share Posted March 30, 2012 <?php $total = 0; foreach{$array1 AS $k=>$v){ $total += $v*$array2[$k]; } echo $total; ?> The OP stated that he/she was only providing two as an example. That could be edited for multiple arrays, but not if the number is dynamic. Plus, if there are keys in array 2 that don't appear in array 1, they would be missed (although that may not be an issue). @op: You are getting these values from somewhere (a DB perhaps?). There is a very good chance you can do what you need when you are extracting the records and are making this more difficult than you need to. But, with what you have stated I have another approach that should work no matter how many arrays you have or which keys each array does or does not have $arry1 = array(1, 5); $arry2 = array(2, 6); //Put all data arrays into parent array $arraysList = array($arry1, $arry2); //Put values into new combined mufti-dimensional array //With the values in sub-arrays by common keys $masterArray = array(); foreach($arraysList as $array) { foreach($array as $key => $value) { $masterArray[$key][] = $value; } } //Calculate product of each value set and add to total $total = 0; foreach($masterArray as &$valueSet) { $total += array_product($valueSet); } echo $total; I'm sure this approach would work, but my function accepts two array parameters. The arrays are just added to the function dynamically. Sorry for the confusion. Quote Link to comment Share on other sites More sharing options...
Jessica Posted March 30, 2012 Share Posted March 30, 2012 The OP stated that he/she was only providing two as an example. That could be edited for multiple arrays, but not if the number is dynamic. Plus, if there are keys in array 2 that don't appear in array 1, they would be missed (although that may not be an issue). true, I was just trying to provide a jumping off point. I kind of expected him to come back and go "now how do I do it for more arrays" :-P Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.