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
https://forums.phpfreaks.com/topic/36085-adding-numbers-in-an-array/
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?

Archived

This topic is now archived and is closed to further replies.

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