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. Link to comment https://forums.phpfreaks.com/topic/54542-solved-average-of-numbers-in-a-txt-file/ 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. Link to comment https://forums.phpfreaks.com/topic/54542-solved-average-of-numbers-in-a-txt-file/#findComment-269814 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. Link to comment https://forums.phpfreaks.com/topic/54542-solved-average-of-numbers-in-a-txt-file/#findComment-269904 Share on other sites More sharing options...
thebestone Posted June 7, 2007 Author Share Posted June 7, 2007 Anyone have an idea? Link to comment https://forums.phpfreaks.com/topic/54542-solved-average-of-numbers-in-a-txt-file/#findComment-270175 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. Link to comment https://forums.phpfreaks.com/topic/54542-solved-average-of-numbers-in-a-txt-file/#findComment-270185 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"; Link to comment https://forums.phpfreaks.com/topic/54542-solved-average-of-numbers-in-a-txt-file/#findComment-270187 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); ?> Link to comment https://forums.phpfreaks.com/topic/54542-solved-average-of-numbers-in-a-txt-file/#findComment-270189 Share on other sites More sharing options...
thebestone Posted June 7, 2007 Author Share Posted June 7, 2007 Thanks everyone. Link to comment https://forums.phpfreaks.com/topic/54542-solved-average-of-numbers-in-a-txt-file/#findComment-270192 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.