artoak Posted August 17, 2009 Share Posted August 17, 2009 I have managed to come up with the code to sum matching elements in 2 or more arrays of variable length. I was hoping the line $return[$k] += $v; (near the bottom of the code) could be changed to $return[$k] *= $v; in order to find the product of array elements from different arrays. It however outputs zero's. My PHP isn't strong enough to understand why this small alteration shouldn't work. Any ideas would be greatly appreciated. <?php $year = array(0 => 21, 1 => 28); $ydata = array(0 => 16, 1 => 21); // $ydata2 = array(0 => 5, 1 => 6); // prints an element function myPrint($value) { print "$value "; } //add every element of 1 array with matching element of different array $product_array = array_product_values($year, $ydata); array_walk($product_array, "myPrint"); function array_product_values() { $return = array(); $arrArgs = func_get_args(); foreach($arrArgs as $arrItem) { foreach($arrItem as $k => $v) { if(!isset($return[$k])) { $return[$k] = 0; } $return[$k] += $v; // it appears '$return[$k] *= $v;' doesn't work } } return $return; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/170751-product-of-matching-array-elements-from-2-or-more-variable-length-arrays/ Share on other sites More sharing options...
p2grace Posted August 17, 2009 Share Posted August 17, 2009 What are the values of $return[$k] and $v? Quote Link to comment https://forums.phpfreaks.com/topic/170751-product-of-matching-array-elements-from-2-or-more-variable-length-arrays/#findComment-900519 Share on other sites More sharing options...
sasa Posted August 18, 2009 Share Posted August 18, 2009 change line $return[$k] = 0; to $return[$k] = 1; Quote Link to comment https://forums.phpfreaks.com/topic/170751-product-of-matching-array-elements-from-2-or-more-variable-length-arrays/#findComment-900624 Share on other sites More sharing options...
artoak Posted August 18, 2009 Author Share Posted August 18, 2009 What are the values of $return[$k] and $v? The values will be integers, stepped through in array's of equal size. In this example the 2 arrays are (21, 28) and (16, 21). Quote Link to comment https://forums.phpfreaks.com/topic/170751-product-of-matching-array-elements-from-2-or-more-variable-length-arrays/#findComment-900779 Share on other sites More sharing options...
artoak Posted August 18, 2009 Author Share Posted August 18, 2009 change line $return[$k] = 0; to $return[$k] = 1; Hi Sasa, that didn't work. the output is (0, 49) so even with *= rather than += it is still summing the second array point although ignoring the first. Any other ideas. Kind regards Quote Link to comment https://forums.phpfreaks.com/topic/170751-product-of-matching-array-elements-from-2-or-more-variable-length-arrays/#findComment-900783 Share on other sites More sharing options...
sasa Posted August 18, 2009 Share Posted August 18, 2009 this code <?php $year = array(0 => 21, 1 => 28); $ydata = array(0 => 16, 1 => 21); // $ydata2 = array(0 => 5, 1 => 6); // prints an element function myPrint($value) { print "$value "; } //add every element of 1 array with matching element of different array $product_array = array_product_values($year, $ydata); array_walk($product_array, "myPrint"); function array_product_values() { $return = array(); $arrArgs = func_get_args(); foreach($arrArgs as $arrItem) { foreach($arrItem as $k => $v) { if(!isset($return[$k])) { $return[$k] = 1; } $return[$k] *= $v; // it appears '$return[$k] *= $v;' doesn't work } } return $return; } ?> return 336 588 is it ok? Quote Link to comment https://forums.phpfreaks.com/topic/170751-product-of-matching-array-elements-from-2-or-more-variable-length-arrays/#findComment-901206 Share on other sites More sharing options...
artoak Posted August 18, 2009 Author Share Posted August 18, 2009 this code <?php $year = array(0 => 21, 1 => 28); $ydata = array(0 => 16, 1 => 21); // $ydata2 = array(0 => 5, 1 => 6); // prints an element function myPrint($value) { print "$value "; } //add every element of 1 array with matching element of different array $product_array = array_product_values($year, $ydata); array_walk($product_array, "myPrint"); function array_product_values() { $return = array(); $arrArgs = func_get_args(); foreach($arrArgs as $arrItem) { foreach($arrItem as $k => $v) { if(!isset($return[$k])) { $return[$k] = 1; } $return[$k] *= $v; // it appears '$return[$k] *= $v;' doesn't work } } return $return; } ?> return 336 588 is it ok? You are correct, thank you. I did make that change but got the answer as I mentioned. Must have been a browser refresh issue, or of course me. My next task is to multiply element n by the preceding element (n-1) in the same array ($year), the n-1 element in array ($ydata) and then sum nth element in ($ydata). How does this work with setting the key to 0 or 1? Or would you advise doing this calculation as 3 separate steps? Quote Link to comment https://forums.phpfreaks.com/topic/170751-product-of-matching-array-elements-from-2-or-more-variable-length-arrays/#findComment-901251 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.