1

I am learning php as part of my studies and at the moment I want to read a csv file into an array and then count all the values. I have successfully read the file and can display all the values in the csv file but am unable to sum/ add them in order to find a total.

Here is my code so far:

   <?php
            $data= explode(",",
              file_get_contents('https://www.mywebsite.com/test.csv')
            );
    $total = 0;
            $lengthofarray=count($data);

            for($x=0;$x<=$lengthofarray;$x++)
            {
                $total = $total + $x; 
//I am not sure if I am missing something here in order to make it working
            }
            echo "  ".$total."<br/>";
    ?>

I know that this is a basic question but I have spent more than 12 hours in order to achieve the solution and have search internet to find the solution but unable to do so.

Here are the values from my csv file:

0.78
0.19
0.78
0.98
0.65
0.79
0.34
0.29
0.55
0.95

1 Answer 1

1

You used $x (the iterator) instead of the $data you got out of the file :)

To make sure PHP treats the $data as int - cast it:

 <?php
    $data= explode("\n",file_get_contents('https://www.mywebsite.com/test.csv'));
    $total = 0;
    $lengthofarray=count($data);

    for($x=0;$x<=$lengthofarray;$x++) {
       $total = $total + (int)$data[$x];
    }
    echo "  ".$total."<br/>";
 ?>

But the better way would be to use foreach:

$data= explode("\n",file_get_contents('https://www.mywebsite.com/test.csv'));
$total = 0;

foreach($data as $current) {
    $total += $current;
}
echo "  ".$total."<br/>";

To load a .csv file there is fgetcsv():

$data = fgetcsv(fopen('test.csv','r'));

Update: Now you posted your .csv: You need to use a new line as separator and not a comma :) edited my samples - and the new best way would be to use file()

$data= file('https://www.mywebsite.com/test.csv');
$total = 0;

foreach($data as $current) {
    $total += $current;
}

echo "  $total<br>";
Sign up to request clarification or add additional context in comments.

6 Comments

Hi Marc Thanks for the suggestions but when I use your code $data= explode(",",file_get_contents('mywebsite.com/test.csv')); $total = 0; foreach($data as $current) { $total += $current; } echo " ".$total."<br/>"; It gives me only value at first position in the array. Also I am not sure about fgetcsv(...) that should I replace the first line in my code? If yes then I get error message when I run the code
Could you post your .csv? Make sure, that you open a file handler while using fgetcsv (and use a relative path)
Yeah I have managed to resolve the error but my loop runs only once and gives me the value at array[0] and thats it. I have a csv with only one columns and more than 2000 rows and I want to add all the rows in the csv file.
Can you post your current code and the .csv? (at least 10 entries so I can see if there is something wrong with my code)
Here is the code I am using: $data= fgetcsv(fopen('mywebsite.com/test.csv')); $total = 0; foreach($data as $current) { $total += $current; } echo " ".$total."<br/>"; And for CSV file I am not sure how to upload it here as there is no option to upload a file...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.