Jump to content
FreakusBzzz

How to short hand this

Recommended Posts

How do I re write my code so it's 



   

 $itemAnnual0 = $values[0] * $periods[0];
    $itemAnnual1 = $values[1] * $periods[1];
    $itemAnnual2 = $values[2] * $periods[2];
...

$itemAnnual79 = $values[79] * $periods[79];

 

 

And for my previous post, I solved it.
 

Share this post


Link to post
Share on other sites

A simple for loop would do the trick.

 

Share this post


Link to post
Share on other sites
Quote

How to short hand this

$values and $periods are already arrays. do the same for $ItemsAnnual and make it an array and populate its values using a loop.

 

Share this post


Link to post
Share on other sites

Sorry, I meant:


or ($i = 0; $i <= 6; $i++) {
    $incomeSubtotal += $values[$i] * $periods[$i];
    $itemAnnual[i] = $values[$i] * $periods[$i];
 }

it no longer works,

so   $itemAnnua[i]l doesnt work when i=2

but  $itemAnnual2  does.
 
 

Share this post


Link to post
Share on other sites

$itemAnnua ?

[ i ]  ??

Share this post


Link to post
Share on other sites

try

$itemAnnual = [];
foreach ($values as $k => $value) {
    $itemAnnual[$k] = $value * $periods[$k];
}

if you only want a single $itemAnnual total then try

$itemAnnual = 0;
foreach ($values as $k => $value) {
    $itemAnnual += $value * $periods[$k];
}

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.