convert a 2d array into a csv file in php - 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);

Related

How do I read the last line to the first file in PHP?

I have it where it prints the file to the body but I need to do that but have the file's data load from the last line to the first.
The code I have right now
$file = fopen('text.txt','r');
while ($line = fgets($file)) {
echo($line);
}
fclose($file);
Use file() to read the file into an array of lines. Then you can reverse the array and loop through it.
$lines = file('text.txt');
$lines = array_reverse($lines);
foreach ($lines as $line) {
echo $line;
}

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);

PHP array into a CSV file

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.

Create CSV and save on local/server automatically

I need to create a CSV file in PHP and that created file should be saved on local/server rather than asking to save.
For the PHP versions later than 5.1.0 you can use the built-in CSV functions.
<?php
$data_array = array (
array ('col1','col2'),
array ('2','2'),
array ('3','6'),
array ('4','2'),
array ('6','5')
);
$fp = fopen('csvfile.csv', 'w');
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
?>
This code will do the trick
$data_array = array (
array ('1','2'),
array ('2','2'),
array ('3','6'),
array ('4','2'),
array ('6','5')
);
$csv = "col1,col2 \n";//Column headers
foreach ($data_array as $record){
$csv.= $record[0].','.$record[1]."\n"; //Append data to csv
}
$csv_handler = fopen ('csvfile.csv','w');
fwrite ($csv_handler,$csv);
fclose ($csv_handler);
echo 'Data saved to csvfile.csv';
but be sure about permission of csvfile so your script can create or write on it

Categories