FWRITE the content of an array ? Help ^_^

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
derceto
Forum Newbie
Posts: 1
Joined: Sun Aug 13, 2006 8:29 am

FWRITE the content of an array ? Help ^_^

Post by derceto »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello!

So, it might sound pretty easy, but I can't figure out how to export the content of an array to a file.
I use :

Code: Select all

<?php
$filename = 'book.dat';
$list = $_SESSION['book'];
$handle = fopen($filename, 'wb');
fwrite($handle, $list);
fclose($handle);
?>
When I open the file saved on the hard drives, instead of including all the records of the 'book' array, it only ouptuts :
"array"
weights 5 bytes!

Oh btw, will this work to get back the content of the string :

Code: Select all

<?php
$filename = 'book.dat';
$handle = fopen ($filename, "rb");
$contents = fread ($handle, filesize ($filename));
fclose ($handle);
echo $contents;
?>
Thanks for your help ... :D


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

is $_SESSION['book'] an array of strings? If so, try

Code: Select all

foreach($_SESSION['book'] as $b) {
	fwrite($handle, $b);
	fwrite($handle, "\n");
}
derceto wrote:$contents = fread ($handle, filesize ($filename));
see http://de2.php.net/file_get_contents
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Or if you want it in an arry when you open it just use file().
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Also check out implode()
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

and var_export($array,true)...
and serialize()...

Image
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Well, if I had an array of strings, and had to write it to a file, I'd file_put_contents($filename, implode("\n", $array));
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

AC, what if those strings could have \n's in them?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Aha. My goof, I thought we were talking about an array of lines (I even said array of strings, hmmm). This does not seem to be the case, rereading the parent post.

Use serialize(). Definitely.
Post Reply