wkilc Posted November 15, 2008 Share Posted November 15, 2008 Hello, If I have an existing file on my server: "tickets.txt" ...each row contains a single number, such as: 1 2 1 2 3 1 ... is it possible to use PHP to display the sum total of all the rows? (10, in this case)? Can someone nudge me in the right direction? Thanks. ~Wayne Link to comment https://forums.phpfreaks.com/topic/132819-solved-sum-of-values-in-text-file/ Share on other sites More sharing options...
GingerRobot Posted November 15, 2008 Share Posted November 15, 2008 A simple combination of file and array_sum should do the trick. Link to comment https://forums.phpfreaks.com/topic/132819-solved-sum-of-values-in-text-file/#findComment-690741 Share on other sites More sharing options...
wkilc Posted November 15, 2008 Author Share Posted November 15, 2008 Thank you! <?php $a = array(1, 2, 1, 2, 3, 1); echo "sum(a) = " . array_sum($a) . "\n"; ?> I'm not sure how to implement the "file". I'm sorry... could some give me an example? ~Wayne Link to comment https://forums.phpfreaks.com/topic/132819-solved-sum-of-values-in-text-file/#findComment-690762 Share on other sites More sharing options...
.josh Posted November 15, 2008 Share Posted November 15, 2008 The manual provides an example. Follow the link. Link to comment https://forums.phpfreaks.com/topic/132819-solved-sum-of-values-in-text-file/#findComment-690765 Share on other sites More sharing options...
wkilc Posted November 15, 2008 Author Share Posted November 15, 2008 Thank you... I didn't see the links. In case anyone as dumb as me searches this thread one day, here's the solution: <?php $lines = file('test.txt/'); echo "sum($lines) = " . array_sum($lines) . "\n"; ?> ~Wayne Link to comment https://forums.phpfreaks.com/topic/132819-solved-sum-of-values-in-text-file/#findComment-690821 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.