Jump to content

Multiply Array Values from Multiple Arrays


unemployment

Recommended Posts

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.

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.