Jump to content

Writing array content to text file?


davocold

Recommended Posts

if its 1-d

foreach($array as $val){
fwrite($testfile, $val);
}

 

if 2-d

foreach($array as $row){
foreach($row as $column){
fwrite(testfile, $column);
}
}

 

you get the idea

 

i think i need the 2d solution but i can not seem to get it right, could u explain it a bit?

chears

foreach goes through the first array (an array of arrays) each element (in that case $row) is also an array, so you go through each of those arrays to get the value. then you write the value to the page.

 

can i see your code?

foreach goes through the first array (an array of arrays) each element (in that case $row) is also an array, so you go through each of those arrays to get the value. then you write the value to the page.

 

can i see your code?

 

$fp = fopen('textfile.txt', 'w');

fwrite($fp, '$myarray');

fclose($fp);

 

 

array contains:

 

red,brown,green,black

black,green,red,brown

green,black,brown,red

 

 

 

 

i want to write the arry to the text file exactly as it is.

oh testfile was a misspelling (always mix up my s's and x's) it should be textfile in your case. but you declare $row and $column in the foreach loop. they are loop variables. I suggest you read a tutorial on foreach loops (there is one in my sig that goes over it briefly, as well as other array stuff)

try

<?php
//$myarray=array(array(1,2,3),2,array('sasa','red'), 3);
echo "before writing to file<br />\n";
echo "<pre>"; 
echo print_r($myarray);
echo "</pre>\n";
$fp = fopen('textfile.txt', 'w');
fwrite($fp, serialize($myarray));
fclose($fp);
echo '<hr />';
$newarray = unserialize(file_get_contents('textfile.txt'));
echo "after readin from file<br />\n";
echo "<pre>", print_r($newarray), "</pre>\n";

?>

try

<?php
//$myarray=array(array(1,2,3),2,array('sasa','red'), 3);
echo "before writing to file<br />\n";
echo "<pre>"; 
echo print_r($myarray);
echo "</pre>\n";
$fp = fopen('textfile.txt', 'w');
fwrite($fp, serialize($myarray));
fclose($fp);
echo '<hr />';
$newarray = unserialize(file_get_contents('textfile.txt'));
echo "after readin from file<br />\n";
echo "<pre>", print_r($newarray), "</pre>\n";

?>

 

 

it works but it is writing some extra chars with the array contents.

anyway to clean this up?

well thats what serialize does. How are you doing to be using this textfile? if you just want to store the value of the array so you can access the array again (and use it like an array) than sasa's script is the way to go

 

isn't there any way i can dump everything in the array to  a text file without adding any extra characters or line breaks?

this is not working. :(

 

i think i should try a diffrent aproach.

 

how do i replace a particular line in a text file with an array string?

 

example:

 

text content is like this:

 

red,brown,green,black

black,green,red,brown

green,black,brown,red

 

so if i have $array = "purple,grey,pink,cyan";

 

how can i use fwrite to replace line 2 in text file which is green,black,brown,red with $array

 

i want to replace the whole line 2 with the arrray string.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.