What's Here?
Members: 136,163
Replies: 478,523
Topics: 74,527
Snippets: 2,566
Tutorials: 675
Total Online: 1,905
Members: 101
Guests: 1,804
Loading. Please Wait...
Write array to file
dinci5
5 Dec, 2007 - 12:58 PM
New D.I.C Head
Joined: 19 Oct, 2006
Posts: 33
My Contributions
Hi
I want to send the data from a form to a file
but I first want to put that data into an array
and then send the array to the file
I need it so when I read the data from the, to put it again in an array
I don't know if this is possible thou
this is what I have so far
CODE
<?php $bestand = 'test.txt'; $file = fopen($bestand, 'a+') or die('Bestand kon niet geopend worden!'); $naam = "".$_POST['naam']." "; $voornaam = "".$_POST['voornaam']." "; $adres = "".$_POST['adres']." "; $pcode = "".$_POST['pcode']." "; $woonplaats = "".$_POST['woonplaats']." "; $telnr = "".$_POST['telnr']."<br />"; $gegevens = array($naam,$voornaam,$adres,$pcode,$woonplaats,telnr); fwrite($file, $gegevens) or die('Kan niet schrijven naar bestand!'); fclose($file); $fileread = 'test.txt'; $data = file($fileread) or die ('Kon bestand niet lezen'); foreach($data as $array){ echo $array; } ?>
but the problem is, I only get the word 'Array' on screen
nothing else.
I want the data from that file again in an array when reading it.
Is this possible?
/EDIT
The language of the variables is Dutch
I'm sorry for that
This post has been edited by dinci5 : 5 Dec, 2007 - 12:59 PM
dinci5
RE: Write Array To File 5 Dec, 2007 - 03:11 PM
New D.I.C Head
Joined: 19 Oct, 2006
Posts: 33
My Contributions
I'm sorry, but I don't understand.
how can I use the print_r() function?
I only want to write that array to a file.
I tried something like this:
CODE
$gegevens = array($naam,$voornaam,$adres,$pcode,$woonplaats,telnr); $test = print_r($gegevens); fwrite($file, $test) or die('Kan niet schrijven naar bestand!');
I get this as output:
QUOTE
Array ( [0] => name [1] => myname [2] => some street [3] => 1111 [4] => Hoboken [5] => telnr ) 1
BUT, I get only a '1' in my file.
I'm not that experienced in PHP coding.
I'm still learning.
Can you please be more specific.
This post has been edited by dinci5 : 5 Dec, 2007 - 03:12 PM
dinci5
RE: Write Array To File 6 Dec, 2007 - 02:29 AM
New D.I.C Head
Joined: 19 Oct, 2006
Posts: 33
My Contributions
QUOTE(nav33n @ 5 Dec, 2007 - 10:25 PM)
Try this.
CODE
<?php $arr=array("blah1","blah2","blah3"); $fp=fopen("test.txt","w+"); foreach($arr as $key => $value){ fwrite($fp,$value."\t"); } ?>
Hope it helps.
yeah...
that worked. Thank you.
I can write an array to a file.
But the rest didn't work as expected.
I wanted to write to it with TR and TD tags and when I read from that file, that everything is in a nice table
But I will figure it out, I hope
Thans again
dinci5
RE: Write Array To File 6 Dec, 2007 - 03:56 AM
New D.I.C Head
Joined: 19 Oct, 2006
Posts: 33
My Contributions
QUOTE(nav33n @ 6 Dec, 2007 - 04:27 AM)
Thats pretty easy.. open a html file, use the file pointer $fp to insert table tags.
cheers,
Naveen
oh
I allready figured it out
I forgot the TABLE tags
I was so focused on the array thing that I forgot the table tags
Now everything works just perfect
Thanks again for your help.
XIONI
RE: Write Array To File 3 Mar, 2008 - 09:23 PM
New D.I.C Head
Joined: 3 Mar, 2008
Posts: 2
hello,
i am a new member to the forum. i have some (very little) experience with php.
i tend to code in languages such as fortran where saving arrays to files and then injecting them back into an array is straight-forward.
suppose we're following what's gone on in this thread here.
so suppose i have used the following code to save an array to a text file:
CODE
<?php $arr=array("blah1","blah2","blah3"); $fp=fopen("test.txt","w+"); foreach($arr as $key => $value){ fwrite($fp,$value."\t"); } ?>
and suppose i want to open the file and recreate the identical array that i originally saved.
what sort of straight-forward code could i use to do this?
many many thanks!
SpaceMan
RE: Write Array To File 4 Mar, 2008 - 04:22 AM
D.I.C Regular
Joined: 20 Feb, 2003
Posts: 270
is so many ways, one small example.
php
<?php $arr=array("blah1","blah2","blah3"); file_put_contents("test.txt",implode(",",$arr)); $file_c = file_get_contents("test.txt"); $colms = explode(",",trim($file_c)); print_r($colms); ?>
XIONI
RE: Write Array To File 4 Mar, 2008 - 09:19 AM
New D.I.C Head
Joined: 3 Mar, 2008
Posts: 2
QUOTE(SpaceMan @ 4 Mar, 2008 - 05:22 AM)
is so many ways, one small example.
php
<?php $arr=array("blah1","blah2","blah3"); file_put_contents("test.txt",implode(",",$arr)); $file_c = file_get_contents("test.txt"); $colms = explode(",",trim($file_c)); print_r($colms); ?>ahh this is excellent! many thanks.
i suppose one more question ... how can i get this to work with a 2dimensional array? is it easier to stick with a 1dimensional array?
SpaceMan
RE: Write Array To File 4 Mar, 2008 - 09:25 AM
D.I.C Regular
Joined: 20 Feb, 2003
Posts: 270
that easy to, but ease is relative. i have done may types. use them sontimes to cashe DB results, for different reasons question is, what kind ya want? my prefered method is to store in a *.inc as an array, and include it.This post has been edited by SpaceMan : 4 Mar, 2008 - 09:29 AM
SpaceMan
RE: Write Array To File 4 Mar, 2008 - 09:45 AM
D.I.C Regular
Joined: 20 Feb, 2003
Posts: 270
hmm, try to edit and add an exanple the the forum choked on it.
seems forgot one bb tags and lost it.
i redo smaller one, not tested, general outline
php
$write_this = "<?\r\n \$temp_array['name'][\"that\"][] = array(implode(',',$arr),'TEST'=>array($array2)); ";This post has been edited by SpaceMan : 4 Mar, 2008 - 09:56 AM
Reference Sheets
Bye Bye Ads
Monthly Drawing
Top Contributors
Top 10 Kudos This Month