Jump to content

Adding numbers in an array


coolman1985

Recommended Posts

Hello and thank you for taking the time to read my thread.

I am trying to add  numbers I have loaded into an array. I have no problem adding sequential numbers, however when a number is missing I cant do it. Here is an example

Sequential: 1,2,3,4,5,6
The code I wrote

Lets say $files is an array containing numbers 1 through 6 and is in descending order “rsort”.

for($j = 1; $j <= $files[0]; $j++){
$value_high = $num_high + $j;
$num_high = $value_high;
}
$count_high = $GLOBALS['value_high'];
echo $count_high;
this script would output 21

Now what if i wanted to add 1,2,3,5,6.. i skipped 4
answer should be 17, but i dont know how to do that?

thanks agian.
Link to comment
Share on other sites

I could use foreach() and have the script auto detect which number in the sequence I am missing. I need this because my script actually reads files form a directory for example 1.txt, 2.txt, and 3.txt and so on. If a file is deleted I want to be able to know which one. Could I still write something with foreach() to do this?

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.