hello DaniWeb Forum,

I need help with reading a csv.

Here's the file:

product name, color, width, quantity, subtotal
product1, red, 20", 200, $30
proudct2, blue, 30", 10, $12

I've written code that creates this file as an order.

What I'd like to do is read the subtotals and generate a grand total.

So I need to read all of the $data[$k][4] and generate a sum.

Any help would be greatly appreciated.


Here is code that fetches data from .csv file.
You can get data from $data massive.
$file = fopen("file.csv","r");

while ($data=fgetcsv($file,1000,","))
$num = count($data);
print $data[$i]."<br>";

Thanks for your quick response Weber,

Is there a way to only print the last item on each line?

Even better, put them in an array?

I'm familiar with array_sum as a command, but I don't know how to get all the subtotals (the last item in each row) into an array.


I haven't tried but i think that is $data[$i][3] on your example.

I haven't tried but i think that is $data[$i][5]
on your example. Collect all values by for cicle.

Thanks Weber,

No joy yet, but I'll keep after it!


I think weber was suggesting something like

for ($i=0; $i<count($data); i++)
  $subtotal[$i] = $data[$i][4];

Thanks Weber!
Hi Ezzaral,

Thanks for all your help.
I'm still being dense.
You can see the file contents earlier in the post.
Here's the code I have so far.
Could you tell me where I'm going wrong?

$item = file('order.txt');
        foreach($item as $key => $val){
            $data[$key] = explode(",", $val);
        for($k = 0; $k < count($item); $k++){
            $subtotal[$k] = $data[$k][4];
        echo (array_sum($subtotal[$k]));

I keep getting errors saying:

[B]Notice[/B]:  Undefined offset:  1
[B]Warning[/B]:  array_sum() [ function.array-sum ]: The argument should be an array

Thanks for any clues you can give me.

You are calling array_sum with a single value:

echo (array_sum($subtotal[$k]));

instead of the array itself. Use

echo (array_sum($subtotal));

to get the sum of that array.

That alone won't work for your file as you posted it though. You have the column names as the first row and you have dollar signs in from of the subtotal values, so an echo of the array contents yields:

subtotal  $30  $120

which sums to 0. Try the following instead:

$item = file('order.txt');
        foreach($item as $key => $val){
            $data[$key] = explode(",", $val);
        for($k = 1; $k < count($item); $k++){
            $subtotal[$k] = substr($data[$k][4],2);
        foreach($subtotal as $val)
          echo $val.' ';
        echo 'Sum: '.(array_sum($subtotal));

Thanks Ezzaral. You're the best.

You were right on the money.
Some of my amounts could be longer than two digits though, so I came up with this:

$item = file((session_id()."-".($_SESSION ['company']).".txt"));
        foreach($item as $key => $val){
            $data[$key] = explode(",", $val);            
                for($k = 0; $k < count($item); $k++){                          
            $subtotal[$k] = ereg_replace("[^0-9]","",$data[$k][4]);}
    echo (array_sum($subtotal));

What do you think?

Thanks so much for teaching me how to create an array this way. This has really helped me out a lot!!


Your change will work fine, as long as you don't have any decimals in the subtotal, otherwise you'll clear them. If you need to accommodate the decimal you can add it to the filter.

The substring would work for any number of digits - it just returns everything past the "$" character(you had a space in there as well, hence the starting value of 2 rather than 1) - but use whichever method you like as the only important part is getting them into the array as numbers :)

Hi Ezzaral,

After further testing, I'm still getting errors so I'll keep after it.


Hi Essaral,

Figured it out.

EOL tag was wrong so the script wasn't reading the correct data.

Thanks for everything,!!!

Glad you got it working :)

(I went to high school in Broken Arrow btw... )