$arr=array();
$row = -1;
if (($handle = fopen("out.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
for ($c = 0; $c < $num; $c++) {
$arr[$row][$c]= $data[$c];
}
}
fclose($handle);
}
I'm using this code to read excel file data, this code counts elements in a row that are divided by comma (,),
Name, Surname, Num, Tel
Name
Surname
Num
tel
but in one field I have word Orginal, and this code also divides element by that word like this:
Orgina
l
and in that way, I receive wrong elemnts, any help?
In the line
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
the 1000 is the maximum length of data to read, as your data (in the example posted) has more than that, it will split the data.
To allow it to read the data properly you can just leave the second and third parameters as defaults ( null for length - in other words any length and the default delimiter is a comma anyway...
while (($data = fgetcsv($handle)) !== FALSE) {
Related
How can I split a csv file into arrays? If the row starts with the letter M I would create a new array and push next lines to it until I had again a row with letter M.
At this moment I push all staff to same array.
while (($data = fgetcsv($handle, 1000, ';')) !== FALSE) {
if($data[0] == 'M'){
$costumerDetail[$x] = $data;
}
else {
$costumerDetail[] = $data;
}
$x++;
}
fclose($handle);
The main concept is there, using $x to keep track of the array position, but you then go back and just add the next values to the same part level of the array.
This code adds the missing part which is a multidimensional array, so $x is the main thing that keeps track of which set of data you are in and you just add it into that dimension of the array (using [$x][]) till the next M occurs...
$costumerDetail = [[]];
$x = 0;
while (($data = fgetcsv($handle, 1000, ';')) !== FALSE) {
if($data[0] == 'M'){
$x++;
$costumerDetail[$x] = [];
}
$costumerDetail[$x][] = $data;
}
fclose($handle);
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.
I'm just wondering is it possible to have a csv file (with many values) and scan the file for a specific value with php?
Ideally if this is possible, Id also like to get the index of that value in the file (i.e what row number the value falls on).
I'm quiet new to php so I'm not sure where to start.
All help is greatly appreciated.
Sure,
Not tested:
<?php
$search = 'mysearchstring';
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE)
{
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$num = count($data);
$row++;
for ($c=0; $c < $num; $c++)
{
if ($data[$c] == $search)
{
echo "Found in row $row in column {$c+1} <br />\n";
}
}
}
fclose($handle);
}
?>
I currently have some code like this:
$handle = fopen($_FILES['file']['tmp_name'], "r");
$i = 0;
while (($data = fgetcsv($handle, 1000, ",")) !== false) {
if($i > 0) {
$sql = "
insert into TABLE(A, B, C, D)
values ('$data[0]', '$data[1]', '$data[2]', '$data[3]')
";
$stmt = $dbh -> prepare($sql);
$stmt->execute();
}
$i++;
}
fclose($handle);
This allows me to write to a certain table the contents of a CSV file, excluding the first row where all the names are. I want to be able to extract only the filled rows. How would I use so using this code?
fgetcsv returns an array consisting of a single null if the rows are empty
http://www.php.net/manual/en/function.fgetcsv.php
so you should be able to do a check based on that.
if ($data[0]===null)
{
continue;
}
or something like that
fgetcsv() returns an array with null for blank lines so you can do something like below.
$handle = fopen($_FILES['file']['tmp_name'], "r");
$i = 0;
while (($data = fgetcsv($handle, 1000, ",")) !== false) {
if (array(null) === $data) { // ignore blank lines
continue;
}
if($i > 0) {
$sql = "
insert into TABLE(A, B, C, D)
values ('$data[0]', '$data[1]', '$data[2]', '$data[3]')
";
$stmt = $dbh -> prepare($sql);
$stmt->execute();
}
$i++;
}
fclose($handle);
Based on the documentation, fgetcsv will return an array consisting of a single null value for empty rows, so you should be able to test the return value against that and skip blank lines that way.
The following example code will skip processing blank lines. Note that I have changed the file and removed some other logic to make it more easily testable.
<?php
$handle = fopen("LocalInput.txt", "r");
$i = 0;
while (($data = fgetcsv($handle, 1000, ",")) !== false) {
if($data== array(null)) continue;
var_dump($data);
$i++;
}
fclose($handle);
?>
the csv data as the following:
(first line) price url
(second line) $10 src="http://www.test.com/1455/"||src="http://www.test.com/image/1456/"||
(third line) $20 src="http://www.test.com/2260/"||src="http://www.test.com/2261/"||
.... ........
now i want to put all the data from the url column into a text file. and shows the data one by one. namely:
http://www.test.com/1455/
http://www.test.com/image/1456/
http://www.test.com/2260/
http://www.test.com/2261/
....
now,i am stucked.
a, i don't know how to prevent outputting the first line.
b,i don't know how to get the url, and put them i a text file one by one.
first i using the following code to output the data to the screen.
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
echo $data[1] . "<br />\n";
}
fclose($handle);
}
the above code also output the first line (url). i don't want to output it. how should i do.
thank you.
You're very close! I have modified your code just slightly to do what you're trying to do. Essentially you need a second file handler for the output. You can open that as append mode so that write append to the end of the file. As for not echoing the header, you just make sure that row is greater than the first row. Hope it helps!
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE && ($handle2 = fopen("output.txt", 'a')) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if($row > 1) {
fwrite($handle2, "{$data[1]}\n");
}
$row++;
}
fclose($handle);
fclose($handle2);
}
// Skips the first line of the CSV - outputting the second "column" of data.
$row = 0;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
if ($row > 0 ) {
echo $data[1] . "<br />\n";
}
$row++;
}
fclose($handle);
}