SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Summing The Results Of An Array In PHP

    Hi,

    I've got the following code that opens a text file and echos out some numbers from each line in the file. I now want to sum the numbers that I've extracted but I'm not sure how? Can anyone help?

    <?php
    $file = fopen('test.txt','r');
    if (!$file)
    {
    Die('FAILED to open file. Are you sure it exists?');
    }
    while (!feof($file))
    {
    $line = fgets($file, 1024);
    $data = explode(' ',$line);
    echo "{$data[2]}<br />";
    }

    fclose($fid);


    ?>

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,014
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    Hi php_geekette, welcome to the forums,

    PHP has a function for that
    http://us.php.net/function.array_sum

  3. #3
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi - thanks for your response - I have previously reviewed the array_sum function but cannot seem to get it to sum all the lines that my code echos out. It may be how I am writing it....I'm completely new to PHP? Are you able to help me with where I should include it in my code?

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,014
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    What does test.txt (at least a few lines) look like?

  5. #5
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's just an example - but I want to sum the 12345 numbers at the end of each line. So if I ran it on the 4 lines below the answer would = 61,725:

    101.231.231.105 -- ABC Testing ABC 12345
    207.3.335.52 -- ADF Testing ABC 12345
    51.4.450.113 -- FRG Testing ABC 12345
    321.134.52.171 -- GHJ Testing ABC 12345
    97.212.678.181 -- HJK Testing ABC 12345

  6. #6
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First of all you have the wrong index what you are echoing. Should be $data[5].

    Here is a one way to do it.
    PHP Code:
    <?php
    $sum 
    0;
    $file fopen('test.txt','r');
    if (!
    $file) { Die('FAILED to open file. Are you sure it exists?'); }
    while (!
    feof($file))
    {
        
    $line fgets($file1024);
        
    $data explode(' ',$line);
        
    $sum += $data[5]; 
        
    // Or you could use intval($data[5]) also to make sure all non numbers will be handled also as number zero.
    }
    fclose($file);
    echo 
    $sum;
    ?>

  7. #7
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks - that's worked and is adding up the numbers correctly.

    I do get a second line that says...

    Notice: Undefined offset: 8 in /filepath/foldername/1.php on line 28

    I'm not sure why?

  8. #8
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Here's an alternate solution.
    PHP Code:
    <?php
    $aValues 
    = array();
    foreach(
    file('test.txt') as $sLine)
    {
        if(
    === preg_match('~[0-9]+$~'$sLine$aMatch))
        {
            
    array_push(
                
    $aValues,
                
    array_shift($aMatch)
            );
        }
    }
    echo 
    array_sum($aValues);
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  9. #9
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you - all is working perfectly now!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •