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

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

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.