Getting a CSV cell full value in PHP - php

Am reading a CSV file using PHP, i get all the values for each row but some columns having long data displays something like this 9.21008E+15
I don't know how to get the complete value which is suppose to be this 9210080000000000
if (($handle = fopen("Test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
$num = count($data);
$tb_row = "";
for ($c=0; $c < $num; $c++) {
$tb_row .= $data[$c].",";
}
$new_row = substr($tb_row, 0, -1);//removing the comma at the end of line...
//explode and assign values then insert
$each_col = explode(',', $new_row);
$Device_state = $each_col[0];
$udid = $each_col[8];
$imsi = $each_col[9];
$imei = $each_col[10];
echo "$imsi || $imei"."<br />";
}
}

Adding a prefix to the cells having this issue solved the issue (')

You need just convert it back into integer value
if (($handle = fopen("Test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
$num = count($data);
$tb_row = "";
for ($c=0; $c < $num; $c++) {
if (strpos($data[$c], 'E+'))
$data[$c] = intval((float)$data[$c]);
$tb_row .= $data[$c].",";
}
$new_row = substr($tb_row, 0, -1);//removing the comma at the end of line...
//explode and assign values then insert
$each_col = explode(',', $new_row);
$Device_state = $each_col[0];
$udid = $each_col[8];
$imsi = $each_col[9];
$imei = $each_col[10];
echo "$imsi || $imei"."<br />";
}
}

Related

How to get csv cell value by row and column values

I have a simple CSV table
Name,Year of birth,City
Yeremy,1980,New York
Louis,1982,Washington
Marta,1987,Chicago
David,1985,Los Angeles
Also, I have this code to can get cell value by number of row and number of column:
$trow = 3;
$tcolumn = 3;
$output .= '<span>';
$row = 1;
$mycsvfile = array();
if (($handle = fopen($abspath, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
$mycsvfile[] = $data;.
}
fclose($handle);
}
$output .= $mycsvfile[$trowN][$tcolumnN];
$output .= '</span>';
I want to define that $trow=Marta and $tcolumn=City, so, in this case to get value "Chicago"...
The code could be optimized a lot, but with the current code:
$output .= array_column($mycsvfile, null, 0)['Marta'][2];
Re-index the array on the values in the Name (column 0)
Access the one with index Marta, getting the value in the City (column 2)
You can access by column name if you combine the column names:
if (($handle = fopen($abspath, "r")) !== FALSE) {
$columns = fgetcsv($handle, 1000, ",");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$mycsvfile[] = array_combine($columns, $data);
}
fclose($handle);
}
$output .= array_column($mycsvfile, null, 'Name')['Marta']['City'];
Keep in mind that if Marta occurs more than once in the Name column then the LAST one will be returned.

PHP changing the index of multidimentional array to value of variable

Pretty new to PHP. I have a csv feed that i am trying to display every line of in the right place. The feed is loaded into a multidimentional array and I can echo out specific places like this.
echo $readcsv[0][2]
But I am trying to use the value of the variable $row in stead of the first number while doing a while loop.
Something like:
echo $readcsv[$row][2]
I have tried next(), str_replace() and strtr() but none of them seems to work while the loop is running.
$row = 1;
$readcsv = array();
if (($handle = fopen("file.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$num = count($data);
echo $readcsv[$row][2];
$row++;
for ($c=0; $c < $num; $c++) {
}
$mycsvfile[] = $data;
}
fclose($handle);
}
CSV file:
title;image_link;link;empty;price;
1;back.gif;http://link.com;;19.95;
2;back.gif;http://link.com;;19.95;
3;back.gif;http://link.com;;19.95;
4;back.gif;http://link.com;;19.95;
5;back.gif;http://link.com;;19.95;
I am not quite sure from your code and question exactly what you are trying to do, but maybe its something like this
if (($handle = fopen("file.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
// you load the line into $data and its a one dimentional array
// occurance 2 is the `link` (http://link.com) if that what you want to echo
echo $data[2];
// as you have a loop, maybe you were trying to
// echo each fields from the csv so you can do that like this
foreach ($data as $idx => $value) {
echo "Column $idx = $value";
}
$mycsvfile[] = $data;
}
fclose($handle);
}

PHP - parse a csv file and write output to another csv file

I am working on Magento products csv file and i have a csv file with 3 columns like
<pre>Name, description, Color</pre>
<pre>t-shirt, tshirt description, "green, blue, yellow"</pre>
Now based on the values of column 3rd column (color) i want to output each row in a separate file say "new_products.csv" in a manner so the resultant file would be like
<p>Name, description, Color</p>
<pre>t-shirt, tshirt description, green</pre>
<pre>t-shirt, tshirt description, blue</pre>
<pre>t-shirt, tshirt description, yellow</pre>
i have started with this code
$row = 1;
if (($handle = fopen("product.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$name = $data[0];
$desc = $data[1];
for ($c=0; $c < $num; $c++) {
$fp = fopen('new_products.csv', 'w');
if($c == 2){ //if we are at color field
$color = explode(',', $data[$c]);
$color_count = count($color);
for($i=0; $i<$color_count; $i++){
fputcsv($fp, array($name,$desc,$color[$i]));
}
}
}
$row++;
}
fclose($handle);
}
However the above code only outputting the last row only.
Thanks
Finally i have been able to find the solution my self. Actually one need to set the file pointer at the end of the file.
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$name = $data[0];
$desc = $data[1];
for ($c=0; $c < $num; $c++) {
if($c == 2){ //if we are at color field
$fp = fopen('file.csv', 'a'); //Open for writing only; place the file pointer at the end of the file.
$color = explode(',', $data[$c]);
$color_count = count($color);
for($i=0; $i<$color_count; $i++){
fputcsv($fp, array($name,$desc,$color[$i]));
}
fclose($fp);
}
}
$row++;
}
fclose($handle);
}

formatting fgetcsv output

Im trying to figure out how to take the data returned by fgetcsv, and format it into a readable/editable table, and then use fputcsv to save that table
so far i have this
<?php
$row = 1;
$handle = fopen("csv.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "\n";
}
}
fclose($handle);
?>
The following code will output the CSV in an html table. To make it editable wrap the echo ..$val.. with tags and add a php form handler that takes the result and reforms a CSV
<?php
$row = 1;
$handle = fopen("csv.csv", "r");
echo("<table>");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
echo("<tr>\r\n");
foreach ($data as $index=>$val) {
echo("\t<td>$val</td>\r\n");
}
echo("</tr>\r\n");
}
echo("</table>");
fclose($handle);
?>

reading first 5 fields of csv file in php

I am confused on how to read a csv file in php ( only the first 4 fields) and exclude the remaining fileds.
Sno,Name,Ph,Add,PO
1,Bob,0102,Suit22,89
2,Pop,2343,Room2,78
I just want to read first 3 fileds and jump to next data. cannot figure out how to do through fgetcsv
any help?
Thanks,
I added a comment to the sample fgetcsv code for you:
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
// iterate over each column here
for ($c=0; $c < $num; $c++) {
// handle column data here
echo $data[$c] . "<br />\n";
// exit the loop after 3rd column parsed
if ($c == 2) break;
}
++$row;
}
fclose($handle);
}
Here's a way to do it without the fgetcsv function:
$lines = file('data.csv');
$linecount = count($lines);
for ($i = 1; $i < $linecount; $i++){
$fields = explode(',', $lines[$i]);
$sno = $fields[0];
$name = $fields[1];
$ph = $fields[2];
$add = $fields[3];
}

Categories