Jump to content

Product of matching array elements from 2 or more variable length arrays


artoak

Recommended Posts

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;
}
?>

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

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?

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.