Solved

Help with fputcsv

Posted on 2008-10-24
4
553 Views
Last Modified: 2010-04-21
I'm exporting an array to a csv file, but it isn't formatting correctly,

This is the array:

Array ( [0] => Array ( [id] => 41 [item] => HY87MB [cost] => $43,679 ) [1] => Array ( [id] => 39 [item] => UYITHO [cost] => $79,202.10 ) )

And the csv file looks like this:
41,HY87MB,$43,679
39,UYITHO,$9,202.10

When, I want it to look like this:
41,"HY87MB","$43,679"
39,"UYITHO","$9,202.10"
/* HERE IS THE CODE I'M USING */
 

$fp = fopen(testfile.csv', 'w');

foreach($result as $line) {

                foreach($line as $line2) {

                                fputcsv($fp, split(',',$line2));

                }

}

fclose($fp);

header('Location: testfile.csv');

Open in new window

0
Comment
Question by:n00b0101
  • 2
  • 2
4 Comments
 
LVL 15

Expert Comment

by:MMDeveloper
ID: 22797527
have you tried forcing the additional arguments?

fputcsv($fp, split(',',$line2), ",", "\"");
0
 

Author Comment

by:n00b0101
ID: 22797702
Yes, I did try that, but I got the same result.
0
 
LVL 15

Accepted Solution

by:
MMDeveloper earned 500 total points
ID: 22798488
as a temporary workaround, try this class. I just wrote it and have not tested it.
<?php
 
 

class putcsv {

	public $fp = NULL;

	public $row = array();
 

	public function putCSV($row) {

		$this->row = $row;
 

		$data = array();
 

		foreach ($row as $k => $v) {

			$data[] = $this->formatCSVCell($v);

		}
 

		fwrite($this->fp, implode(",", $data) . "\n");

	}
 

	private function formatCSVCell($data) {

		$useQuotes = false;
 

		$quotable = array (

				"\"" => "\"\"",

				"," => ",",

				"\n" => "\n",

				"$", "$"

			);
 

		foreach ($quotable as $char => $repl) {

			if (eregi($char, $data)) {

				$useQuotes = true;

			} else {}

		}
 

		if ($useQuotes == true) {

			foreach ($quotable as $char => $repl) {

				$data = str_replace($char, $repl, $data);

			}
 

			$data = "\"" . $data . "\"";

		}

		else {
 

		}
 

		return $data;

	}

}
 

$putcsv = new putcsv();

$putcsv->fp = fopen("file.csv", "w+");
 

foreach ($lines as $line) {

	$putcsv->putCSV($line);

}

fclose($putcsv->fp);

?>

Open in new window

0
 

Author Closing Comment

by:n00b0101
ID: 31509689
thanks for this
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now