thebestone Posted June 7, 2007 Share Posted June 7, 2007 Hello, How would I go about adding the numbers in a txt file and then dividing by however many numbers there are? Thanks for the info. Quote Link to comment Share on other sites More sharing options...
Orio Posted June 7, 2007 Share Posted June 7, 2007 Each number is in a different line? Comma separated? It would be easier to help you if you provide all the info, and maybe a part of the txt file too. Orio. Quote Link to comment Share on other sites More sharing options...
thebestone Posted June 7, 2007 Author Share Posted June 7, 2007 Right now they're all on a separate line but I can change it if another way would be easier. Quote Link to comment Share on other sites More sharing options...
thebestone Posted June 7, 2007 Author Share Posted June 7, 2007 Anyone have an idea? Quote Link to comment Share on other sites More sharing options...
chigley Posted June 7, 2007 Share Posted June 7, 2007 <?php $lines = file("file.txt"); $total = 0; foreach($lines as $line) { $total += $line; } $average = $total / count($lines); ?> Try that. Quote Link to comment Share on other sites More sharing options...
per1os Posted June 7, 2007 Share Posted June 7, 2007 $contents = file('text.txt'); $total = 0; foreach ($contents as $num) { $total += $num; } $average = ($total / count($contents)); echo $average . " average " . $total . " total"; Quote Link to comment Share on other sites More sharing options...
taith Posted June 7, 2007 Share Posted June 7, 2007 dont bother mucking around with loops much faster this way <?php $file=file("file.txt"); $average=array_sum($file)/count($file); ?> Quote Link to comment Share on other sites More sharing options...
thebestone Posted June 7, 2007 Author Share Posted June 7, 2007 Thanks everyone. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.