Write array to file

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 163254 Views - Last Post: 22 October 2011 - 02:10 PM Rate Topic: -----

#1 dinci5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 19-October 06

Write array to file

Post icon  Posted 05 December 2007 - 01:58 PM

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

<?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: 05 December 2007 - 01:59 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Write array to file

#2 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5364
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: Write array to file

Posted 05 December 2007 - 02:13 PM

View Postdinci5, on 5 Dec, 2007 - 01:58 PM, said:

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.

print_r($gegevens);



http://www.php.net/m....array-keys.php
Was This Post Helpful? 0
  • +
  • -

#3 dinci5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 19-October 06

Re: Write array to file

Posted 05 December 2007 - 04:11 PM

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:

$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: 05 December 2007 - 04:12 PM

Was This Post Helpful? 0
  • +
  • -

#7 nav33n  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 71
  • Joined: 02-October 07

Re: Write array to file

Posted 05 December 2007 - 10:25 PM

Try this.
<?php
$arr=array("blah1","blah2","blah3");
$fp=fopen("test.txt","w+");
foreach($arr as $key => $value){
fwrite($fp,$value."\t");
}
?>

Hope it helps. :)
Was This Post Helpful? 1

#8 dinci5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 19-October 06

Re: Write array to file

Posted 06 December 2007 - 03:29 AM

View Postnav33n, on 5 Dec, 2007 - 10:25 PM, said:

Try this.
<?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 :P


Thans again :)
Was This Post Helpful? 0
  • +
  • -

#9 nav33n  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 71
  • Joined: 02-October 07

Re: Write array to file

Posted 06 December 2007 - 04:27 AM

Thats pretty easy.. open a html file, use the file pointer $fp to insert table tags. :)
cheers,
Naveen
Was This Post Helpful? 0
  • +
  • -

#10 dinci5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 19-October 06

Re: Write array to file

Posted 06 December 2007 - 04:56 AM

View Postnav33n, on 6 Dec, 2007 - 04:27 AM, said:

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 :D
I was so focused on the array thing that I forgot the table tags
Now everything works just perfect

Thanks again for your help. :)
Was This Post Helpful? 0
  • +
  • -

#11 XIONI  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-March 08

Re: Write array to file

Posted 03 March 2008 - 10:23 PM

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:

<?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!
Was This Post Helpful? 0
  • +
  • -

#12 SpaceMan  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 10
  • View blog
  • Posts: 507
  • Joined: 20-February 03

Re: Write array to file

Posted 04 March 2008 - 05:22 AM

is so many ways, one small example.

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

?>


Was This Post Helpful? 0
  • +
  • -

#13 XIONI  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-March 08

Re: Write array to file

Posted 04 March 2008 - 10:19 AM

View PostSpaceMan, on 4 Mar, 2008 - 05:22 AM, said:

is so many ways, one small example.

<?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. :D

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?
Was This Post Helpful? 0
  • +
  • -

#14 SpaceMan  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 10
  • View blog
  • Posts: 507
  • Joined: 20-February 03

Re: Write array to file

Posted 04 March 2008 - 10:25 AM

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: 04 March 2008 - 10:29 AM

Was This Post Helpful? 0
  • +
  • -

#15 SpaceMan  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 10
  • View blog
  • Posts: 507
  • Joined: 20-February 03

Re: Write array to file

Posted 04 March 2008 - 10:45 AM

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

$write_this = "<?\r\n
\$temp_array['name'][\"that\"][] = array(implode(',',$arr),'TEST'=>array($array2));
";


This post has been edited by SpaceMan: 04 March 2008 - 10:56 AM

Was This Post Helpful? 0
  • +
  • -

#16 Guest_Guest*


Reputation:

Re: Write array to file

Posted 20 January 2011 - 08:47 AM

Try this one:

  $fh = fopen($file, 'w') or die("can't open file");
  fwrite($fh, print_r($all_users, true));
  fclose($fh);
  
  // This wil happen in the update script
  $c = file_get_contents($file);
  print_r($c);



For me, this put the whole array in a file which i then opened later and can use it for whatever purpose
Was This Post Helpful? 0

#17 Brandon_R  Icon User is offline

  • New D.I.C Head

Reputation: 14
  • View blog
  • Posts: 46
  • Joined: 08-January 11

Re: Write array to file

Posted 21 January 2011 - 05:41 AM

Why don't you guys just use http://php.net/manua...n.serialize.php There are code examples on how to store arrays and get them back.
Was This Post Helpful? 0
  • +
  • -

#18 JohnWayne999  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-October 11

Re: Write array to file

Posted 19 October 2011 - 09:07 PM

View PostBrandon_R, on 21 January 2011 - 05:41 AM, said:

Why don't you guys just use http://php.net/manua...n.serialize.php There are code examples on how to store arrays and get them back.


Good idea. Here's how I did it (using bits and pieces of code found online put together). Put together for you including (un)serialise, encode/decode, f_put/get:

<?php
    $array["a"] = "Foo";
    $array["b"] = "Bar";
    $array["c"] = "Baz";
    $array["d"] = "Wom";

    $str = serialize($array); // Serialises the array $array and store as $str
    $strenc = urlencode($str);	// Encode the serialised array

	echo "<br><br> Serialised: " . $str; //serialised string. Basically it's exploding an array into a storable text string.
	echo "<br><br> Serialised and encoded: " . $strenc; //serialised and encoded string that is safe to be passed around on the web
		
	$file = '/tmp/file';
	file_put_contents($file, $strenc);
	
	$content2 = file_get_contents($file);
	
	$arr = unserialize(urldecode($content2));
    var_dump($arr);	
	echo "<br><br> Retrieved array element: " . $arr["c"] ."<br><br>"; //decoded and then unserialised back to original array. If you want to access any element, just ask for it. Just testing here. Answer is Baz.	
		
	foreach ($arr as $value) {echo $value;} //or show all content if you like	
?> 

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2