bothwell Posted August 5, 2008 Share Posted August 5, 2008 I have a while loop that outputs a line of numbers. I want to find the sum of these numbers. I know I can use array_sum() to find the sum of an array's contents, but I can't seem to get my line of numbers into an array in the first place. :/ All the array() tutorials tell me how to populate an array with predefined values, but I don't have those. I know this is just a noob syntax question but I just can't seem to find the right answer anywhere - any help is appreciated $i = 0; $a = 0; $b = 0; $count = 1; $sumSquares = array(); while ($count <=10) { $i = $count * $count; $count++; print $i.", "; // I want this list of numbers to be inside the $sumSquares array below! $sumSquares[$i] = $i; } print "sum: "; print array_sum($sumSquares[$i]); // This is not an array so it doesn't work Link to comment https://forums.phpfreaks.com/topic/118219-solved-noob-question-about-arrays/ Share on other sites More sharing options...
JonnyThunder Posted August 5, 2008 Share Posted August 5, 2008 $sumSquares = array(); for ($count = 1; $count <= 10; $count++) { $sumSquares[] = $count * $count; } print "sum: " . array_sum($sumSquares); Does this not work?? Think the reason yours didn't work is because you were specifying an element in the array when calling array_sum, instead of specifying the whole array. Link to comment https://forums.phpfreaks.com/topic/118219-solved-noob-question-about-arrays/#findComment-608410 Share on other sites More sharing options...
bothwell Posted August 5, 2008 Author Share Posted August 5, 2008 $sumSquares = array(); for ($count = 1; $count <= 10; $count++) { $sumSquares[] = $count * $count; } print "sum: " . array_sum($sumSquares); Does this not work?? Think the reason yours didn't work is because you were specifying an element in the array when calling array_sum, instead of specifying the whole array. Yes, it does, thank you! So what's the difference between using a while and a for loop when doing something like this? I can't really see any functional differences between the two, just shifted syntax. (BTW, when I ask for help here, your name keeps coming up in the responses - just wanted to say thanks for helping out!) Link to comment https://forums.phpfreaks.com/topic/118219-solved-noob-question-about-arrays/#findComment-608422 Share on other sites More sharing options...
JonnyThunder Posted August 5, 2008 Share Posted August 5, 2008 No problem. I guess I changed it to a 'for' loop for clarity. There is no functional difference, but (im my opinion) it makes code look a little tidier. (i'm just a picky programmer I guess! ) The problem you had though, was specifying $i when using array_sum. By doing that, you asked it to count a single element of your array instead of the whole thing. Also, there was no need to specify which element of the array to put your value into. By using [] instead of [$i] a new entry is added to the array automatically, so no need to know specifically where you want to add the value. Link to comment https://forums.phpfreaks.com/topic/118219-solved-noob-question-about-arrays/#findComment-608431 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.