Re: Array to csv or excel in php

From: Date: Mon, 19 Apr 2010 15:52:27 +0000
Subject: Re: Array to csv or excel in php
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-304224@lists.php.net to get a copy of this message
On 19 April 2010 17:40, Andrew Ballard <aballard@gmail.com> wrote: > On Mon, Apr 19, 2010 at 11:14 AM, Peter Lind <peter.e.lind@gmail.com> wrote: >> On 19 April 2010 17:00, Andrew Ballard <aballard@gmail.com> wrote: >>> On Mon, Apr 19, 2010 at 9:45 AM, Manolis Vlachakis >>>>   1. $save=split("[|;]",$listOfItems); >>>> >>>> and what i want i s after making some changes to the attributes on the array >>>> above to export them on an csv or excel format >>>> but directly as a message to the browser .. >>>> i dont want it to be saved on the server ... >>>> >>> Often when outputting csv, I usually do something like this: >>> >>> <?php >>> >>> $fp = fopen('php://output', 'w') or die('Could not open >>> stream'); >>> >>> foreach ($data as $row) { >>>    // Assumes that $row will be an array. >>>    // Manipulate the data in $row if necessary. >>>    fputcsv($fp, $row); >>> } >>> >>> ?> >> >> An interesting idea. I'd do: >> >> echo implode(',', $row); >> > > If it's very simple data that works, but it doesn't allow for the > optional enclosure characters that fputcsv() uses in cases where a > data element includes the column and/or row delimiter characters. I > had originally written something using an array_map callback that did > the optional enclosures as needed and then used echo implode() as you > suggest, but found the solution I posted was shorter and faster. YMMV > > Andrew > Yeah, was considering that point as well. I'd use the echo if the array values are getting modified anyway. Otherwise your solution is probably simpler. Regards Peter -- <hype> WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind Flickr: http://www.flickr.com/photos/fake51 BeWelcome: Fake51 Couchsurfing: Fake51 </hype>

« previous php.general (#304224) next »