Reputation: 101
I am trying to find out the count value from the number of the rows in csv file : this is php script ,
<?php
$str=null;
if (($handle = fopen("rahul.csv", "r")) !== FALSE) {
$i=0;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$str.= "[".$i.",".$data[1]."]," ;
$i++;
}
fclose($handle);
}
echo "$str";
?>
And this is csv file:
18-Jul-14 99.75 99.98
19-Jul-14 99.77 99.97
20-Jul-14 99.78 99.99
21-Jul-14 99.84 99.99
22-Jul-14 99.82 99.99
23-Jul-14 99.82 99.98
24-Jul-14 99.76 99.98
25-Jul-14 99.78 99.98
26-Jul-14 99.8 99.99
27-Jul-14 99.65 99.98
28-Jul-14 99.94 99.99
29-Jul-14 99.8 99.95
31-Jul-14 99.78 99.98
1-Aug-14 99.82 99.99
2-Aug-14 99.82 99.99
3-Aug-14 99.78 99.99
4-Aug-14 99.73 99.99
5-Aug-14 99.84 99.99
6-Aug-14 99.77 99.99
7-Aug-14 99.83 99.99
8-Aug-14 99.83 99.98
9-Aug-14 99.72 99.91
10-Aug-14 99.8 99.99
11-Aug-14 99.82 99.99
12-Aug-14 99.83 99.98
13-Aug-14 99.89 99.98
14-Aug-14 99.87 99.99
15-Aug-14 99.84 99.99
16-Aug-14 99.86 99.99
17-Aug-14 99.86 99.98
When i output this csv file i got the following output:
[0,99.75],[1,99.77],[2,99.78],[3,99.84],[4,99.82],[5,99.82],[6,99.76],[7,99.78],[8,99.8],[9,99.65],[10,99.94],[11,99.8],[12,99.78],[13,99.82],[14,99.82],[15,99.78],[16,99.73],[17,99.84],[18,99.77],[19,99.83],[20,99.83],[21,99.72],[22,99.8],[23,99.82],[24,99.83],[25,99.89],[26,99.87],[27,99.84],[28,99.86],[29,99.86],
Now I am trying to remove the comma (,) from the last value by getting to count and removing the comma for the last value.
I am trying to get the output like this:
[0,99.75],[1,99.77],[2,99.78],[3,99.84],[4,99.82],[5,99.82],[6,99.76],[7,99.78],[8,99.8],[9,99.65],[10,99.94],[11,99.8],[12,99.78],[13,99.82],[14,99.82],[15,99.78],[16,99.73],[17,99.84],[18,99.77],[19,99.83],[20,99.83],[21,99.72],[22,99.8],[23,99.82],[24,99.83],[25,99.89],[26,99.87],[27,99.84],[28,99.86],[29,99.86]
Upvotes: 0
Views: 58
Reputation: 1323
First explode your result to the comma, then, implode your explode result to the comma. The last comma should be remove.
$result = explode(",", $str);
$result = implode(",", $result);
Upvotes: 0
Reputation: 781210
You can just remove the last character with:
$str = substr($str, 0, strlen($str)-1);
But I would have done it a different way in the first place. Put all the strings in an array, and then join them together with implode
.
Or, since your result looks just like JSON, you could use json_encode
.
$arr = array();
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$arr[] = array($i, $data[1]);
$i++;
}
$str = json_encode($arr);
Upvotes: 2
Reputation: 8819
you can remove it by using
rtrim($str, ",");
OR
trim($str, ",");
Upvotes: 2