0

Let say.. I have data like this

wij = [0.5, 0.30, 0.25, 0.15, 0.25]

and

enter image description here

As you see.. each data in wij is represent from C1 until C5 from table above

C1=0.5, C2=0.30, C3=0.25, C4=0.15, C5=0.25

So.. i create the wij into array-variable like this:

$wij = array(0.5, 0.30, 0.25, 0.15, 0.25);

and A1 until A5 rows into array-variable:

$nij = array(
    array(150, 15, 2, 2, 3);
    array(500, 200, 2, 3, 2);
    array(200, 10, 3, 1, 3);
    array(350, 100, 3, 1, 2);
);

I want to multiplying each data from wij with data from A1 until A5 rows, so it will be look like this:

A1 = (0.5*150)+(0.30*15)+(0.25*2)+(0.15*2)+(0.25*3)

A2 = (0.5*500)+(0.30*200)+(0.25*2)+(0.15*3)+(0.25*2)

A3 = (0.5*200)+(0.30*10)+(0.25*3)+(0.15*1)+(0.25*3)

A4 = (0.5*350)+(0.30*100)+(0.25*3)+(0.15*1)+(0.25*2)

I don't have any clue how to do it using for-loops or foreach-loops. Because each rows in table is not always have 4 data like from table above, it can always 5 rows or more, so I guess it will be work if using for-loops.

1 Answer 1

1
<?php 
$wij = array(0.5, 0.30, 0.25, 0.15, 0.25);
$array1 = array(150, 15, 2, 2, 3);
$array2 = array(500, 200, 2, 3, 2);
$array3 = array(200, 10, 3, 1, 3);
$array4 = array(350, 100, 3, 1, 2);
$arrays = array($array1,$array2,$array3,$array4);
$as = array(0,0,0,0);


for($i = 0;$i<4;$i++)
{
    for($t = 0;$t<5;$t++)
    {
    $as[$i] += ($wij[$t]*$arrays[$i][$t]);
    }
    echo "</br>".$as[$i];   
}

?>

I can explain for-loop, if you want.

Sign up to request clarification or add additional context in comments.

2 Comments

interesting style, I would write arrays in array, looks much cleaner than 5 variables insreted in array.
also use foreach loops, better because array size may differ

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.