PHP array into a CSV file - php

my problem is, that I want to save an array into a CSV file.
Here my code:
//Webseite auslesen
$string = file_get_contents("***Zensiert***");
$helper = preg_match_all('!<a.*?href=\"([^\"]*)\"[^>]*>(.*?)</a>!', $string, $matches);
$fl_array = preg_grep('/^http.*/', $matches[1]);
echo '<pre>';
print_r($fl_array);
echo '</pre>';
$fp = fopen('file.csv', 'a');
foreach ($fl_array as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
The file is generated, but without content?!
I didn't see the mistake...
Hope you can help me.

I would change some lines to those
$fp = fopen('file.csv', 'w');
fputcsv($df, array_keys(reset($fl_array))); //Put column names as the 1st row (you may comment this line)
foreach ($fl_array as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
This opens file to write.
And in your code you probably want to change that foreach into: fputcsv($fp, $fl_array );

fputcsv($fp, $fields);
$fields isn't an array.. so that was the problem.

Related

Save exported csv to a location on app storage

I am trying to save some failed records as csv to my laravel app storage. I have gone through this php documentation but how to store the file in a desired location is my challenge.
$failedRecordArrayFile = arrayToCsv($failedRecordArray, "file101.csv");
function arrayToCsv($data, $fileName)
{
$fp = fopen($fileName, 'w');
foreach ($data as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
}
I ran the code block above and checked for the file name "file101.csv" in my entire application but I couldn't find any result.
this would work...
<?php
function arrayToCsv($data, $fileName)
{
$fp = fopen($fileName, 'w');
foreach ($data as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
}
$failedRecordArray = array(array('col1','col2','col3','col4'), array('This','is','a','test'));
$failedRecordArrayFile = arrayToCsv($failedRecordArray, "file101.csv");
?>
file101.csv
col1,col2,col3,col4
This,is,a,test

how to convert php file into csv

$lang['add_a']="Add a";
$lang['ban_zon_link']="Banner, Zone linking";
$lang['ban_zon_link_help']="Zones linking with Banners";
$lang['manage_ban_zon_link_help']="Manage banner zone linking -";
$lang['adtag']="Ad Tag";
I saved above lines into php file as lang.php need to convert php file into csv in code level.
You could open the file as csv, iterate over your array and use fputcsv
foreach($lang as $myrow) {
fputcsv($output, $myrow);
}
http://php.net/manual/en/function.fputcsv.php
You can use fputcsv:
<?php
$lang = ...
$fp = fopen('file.csv', 'w');
// Insert array keys as CSV header
fputcsv($fp, array_keys($lang);
// Insert values as first data row
fputcsv($fp, array_values($fields));
fclose($fp);
?>
This works if you have only row record in your $lang array as your example suggests. This will create a 2 rows CSV with an header (the keys of the $lang array) and one data row (the values of the array.
If you are aiming at a different thing please clarify your question.
include("lang.php");
foreach($lang as $l)
{
file_put_contents("csv_file.csv",$l.",",FILE_APPEND);
// csv_file.csv is the name of your file
}
file_put_contents("csv_file.csv","\n",FILE_APPEND);
try with this:-
/* $lang['add_a']="Add a";
$lang['ban_zon_link']="Banner, Zone linking";
$lang['ban_zon_link_help']="Zones linking with Banners";
$lang['manage_ban_zon_link_help']="Manage banner zone linking -";
$lang['adtag']="Ad Tag";*/
if above php code in lang.php:-
require_once "lang.php";
$fp = fopen('file.csv', 'w');
fputcsv($fp, array_keys($lang));
fputcsv($fp, array_values($lang));
fclose($fp);
To read in a PHP script and write it out, I've done the following...
<?php
$file="part1.php";
$outFile="part1.csv";
$in = file($file);
$out = fopen($outFile, "w");
foreach($in as $line ) {
$parts = explode("=", $line);
if ( count($parts) == 2 ) {
fputcsv($out, array($parts[0],"=",rtrim($parts[1],";".PHP_EOL)));
}
}
fclose($out);
which for your example ( as part1.php) gives...
$lang['add_a'],=,"""Add a"""
$lang['ban_zon_link'],=,"""Banner, Zone linking"""
$lang['ban_zon_link_help'],=,"""Zones linking with Banners"""
$lang['manage_ban_zon_link_help'],=,"""Manage banner zone linking -"""
$lang['adtag'],=,"""Ad Tag"""
$file = 'file.csv';
header( "Content-Type: text/csv;charset=utf-8" );
header( "Content-Disposition: attachment;filename=\"$file\"" );
header("Pragma: no-cache");
header("Expires: 0");
$fp= fopen('php://output', 'w');
foreach ($lang as $fields)
{
fputcsv($fp, $fields);
}
fclose($fp);
exit();

write CSV with fputcsv

When I use this code, the new CVS file get "" by text with blanks:
//array in csv schreiben
$daten = array_filter($daten);
$csv_array[] = $daten;
$fp = fopen("golfDBtest.csv", 'w');
foreach ($csv_array as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
The original CSV:
Reit im Winkel,69.8,131,68.4,126,71.6,129,69.5,127,70,7,4,5,4,13,5,3,4,1,4,9,4,17,3,15,3,11,4,10,4,14,4,18,3,4,4,2,4,8,3,16,5,6,3,12,5,00438640798250
The new file:
"Reit im Winkl",69.8,131,68.4,126,71.6,129,69.5,127,70,7,4,5,4,13,5,3,4,1,4,9,4,17,3,15,3,11,4,10,4,14,4,18,3,4,4,2,4,8,3,16,5,6,3,12,5,00438640798250
How can I write a CVS without ""?
Thanks
According to the documentation of fputcsv function it has a default enclosure value: ". Setting this so blank should help your cause.
I'd say this should work:
//array in csv schreiben
$daten = array_filter($daten);
$csv_array[] = $daten;
$fp = fopen("golfDBtest.csv", 'w');
foreach ($csv_array as $fields) {
fputcsv($fp, $fields, ",", "");
}
fclose($fp);

convert a 2d array into a csv file in php

I'm try to convert a 2d array into a csv file . but i got errors
array to string conversion in line 7
here is my code
<?php
$export_arr =$_POST['dataString'];
$fp = fopen('file.xls', 'w');
foreach ( $export_arr as $line ) {
//$val = explode(",", $line);
fputcsv($fp, $export_arr);
}
fclose($fp);
?>
i want put the values in each cell in the xls file
any ideas
Change this line:
fputcsv($fp, $export_arr);
To:
fputcsv($fp, $line);
It should be:
fputcsv($fp, $line);

how can i make an interface in php to add data csv file

hey...i need to create a html+php+javascript interface
where i got three text area and i want::::
that if i'll enter some data into text area and pressing submit button,,then it'll enter that textarea's data into a csv file..
pls some one help....
<?php
$list = arrary();
//your array will have all the fields from the form, textfields and textareas
$fp = fopen('output.csv', 'w');
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
?>
$fp = fopen('file.csv', 'w');
fputcsv($fp, $_GET['TextArea']);
fclose($fp);
?>

Categories