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 Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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 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.