Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 136,163 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,905 people online right now. Registration is fast and FREE... Join Now!




Write array to file

 
Reply to this topicStart new topic

Write array to file

dinci5
5 Dec, 2007 - 12:58 PM
Post #1

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
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Write Array To File
5 Dec, 2007 - 01:13 PM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,446



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE(dinci5 @ 5 Dec, 2007 - 01:58 PM) *

but the problem is, I only get the word 'Array' on screen
nothing else.

You are getting this because you are referencing the entire array, & not the unique keys inside of the array.

CODE

print_r($gegevens);


http://www.php.net/manual/en/function.array-keys.php
User is online!Profile CardPM
+Quote Post

dinci5
RE: Write Array To File
5 Dec, 2007 - 03:11 PM
Post #3

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. smile.gif

This post has been edited by dinci5: 5 Dec, 2007 - 03:12 PM
User is offlineProfile CardPM
+Quote Post

nav33n
RE: Write Array To File
5 Dec, 2007 - 09:25 PM
Post #4

D.I.C Head
**

Joined: 2 Oct, 2007
Posts: 71


My Contributions
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. smile.gif
User is offlineProfile CardPM
+Quote Post

dinci5
RE: Write Array To File
6 Dec, 2007 - 02:29 AM
Post #5

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. smile.gif



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 tongue.gif


Thans again smile.gif
User is offlineProfile CardPM
+Quote Post

nav33n
RE: Write Array To File
6 Dec, 2007 - 03:27 AM
Post #6

D.I.C Head
**

Joined: 2 Oct, 2007
Posts: 71


My Contributions
Thats pretty easy.. open a html file, use the file pointer $fp to insert table tags. smile.gif
cheers,
Naveen
User is offlineProfile CardPM
+Quote Post

dinci5
RE: Write Array To File
6 Dec, 2007 - 03:56 AM
Post #7

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. smile.gif
cheers,
Naveen


oh
I allready figured it out smile.gif
I forgot the TABLE tags biggrin.gif
I was so focused on the array thing that I forgot the table tags
Now everything works just perfect

Thanks again for your help. smile.gif
User is offlineProfile CardPM
+Quote Post

XIONI
RE: Write Array To File
3 Mar, 2008 - 09:23 PM
Post #8

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!

User is offlineProfile CardPM
+Quote Post

SpaceMan
RE: Write Array To File
4 Mar, 2008 - 04:22 AM
Post #9

D.I.C Regular
Group Icon

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

?>

User is offlineProfile CardPM
+Quote Post

XIONI
RE: Write Array To File
4 Mar, 2008 - 09:19 AM
Post #10

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. biggrin.gif

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?
User is offlineProfile CardPM
+Quote Post

SpaceMan
RE: Write Array To File
4 Mar, 2008 - 09:25 AM
Post #11

D.I.C Regular
Group Icon

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
User is offlineProfile CardPM
+Quote Post

SpaceMan
RE: Write Array To File
4 Mar, 2008 - 09:45 AM
Post #12

D.I.C Regular
Group Icon

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
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 11:58PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month