coolman1985 Posted January 28, 2007 Share Posted January 28, 2007 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 exampleSequential: 1,2,3,4,5,6The code I wroteLets 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 21Now what if i wanted to add 1,2,3,5,6.. i skipped 4answer 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 More sharing options...
Jessica Posted January 28, 2007 Share Posted January 28, 2007 if the array just doesn't have a 4, instead of for, use foreach()If it has 4 and you want to skip it, just do if($i != 4){} Link to comment https://forums.phpfreaks.com/topic/36085-adding-numbers-in-an-array/#findComment-171259 Share on other sites More sharing options...
coolman1985 Posted January 28, 2007 Author Share Posted January 28, 2007 well, it's not allways 4 thats missing it could be 1,3,4 missing 2, or so on. there howerver will never be more than one missing number. Link to comment https://forums.phpfreaks.com/topic/36085-adding-numbers-in-an-array/#findComment-171263 Share on other sites More sharing options...
Jessica Posted January 28, 2007 Share Posted January 28, 2007 So use foreach Link to comment https://forums.phpfreaks.com/topic/36085-adding-numbers-in-an-array/#findComment-171265 Share on other sites More sharing options...
coolman1985 Posted January 28, 2007 Author Share Posted January 28, 2007 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 https://forums.phpfreaks.com/topic/36085-adding-numbers-in-an-array/#findComment-171275 Share on other sites More sharing options...
coolman1985 Posted January 28, 2007 Author Share Posted January 28, 2007 I got it, thank you :D Link to comment https://forums.phpfreaks.com/topic/36085-adding-numbers-in-an-array/#findComment-171298 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.