davocold Posted November 12, 2009 Share Posted November 12, 2009 array contains the following lines: red,brown,green,black black,green,red,brown green,black,brown,red How can i write the whole content of the array to a text file? fwrite($textfile, $array); won't do it Quote Link to comment https://forums.phpfreaks.com/topic/181299-writing-array-content-to-text-file/ Share on other sites More sharing options...
mikesta707 Posted November 12, 2009 Share Posted November 12, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/181299-writing-array-content-to-text-file/#findComment-956378 Share on other sites More sharing options...
sasa Posted November 12, 2009 Share Posted November 12, 2009 you can write string representation of array fwrite($textfile, serialize($array)); Quote Link to comment https://forums.phpfreaks.com/topic/181299-writing-array-content-to-text-file/#findComment-956387 Share on other sites More sharing options...
davocold Posted November 12, 2009 Author Share Posted November 12, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/181299-writing-array-content-to-text-file/#findComment-956390 Share on other sites More sharing options...
mikesta707 Posted November 12, 2009 Share Posted November 12, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/181299-writing-array-content-to-text-file/#findComment-956393 Share on other sites More sharing options...
davocold Posted November 12, 2009 Author Share Posted November 12, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/181299-writing-array-content-to-text-file/#findComment-956408 Share on other sites More sharing options...
mikesta707 Posted November 12, 2009 Share Posted November 12, 2009 did you completely ignore what i wrote? you didn't change anything at all... you just copied your first post and pasted it into a new one Quote Link to comment https://forums.phpfreaks.com/topic/181299-writing-array-content-to-text-file/#findComment-956414 Share on other sites More sharing options...
davocold Posted November 12, 2009 Author Share Posted November 12, 2009 did you completely ignore what i wrote? you didn't change anything at all... you just copied your first post and pasted it into a new one how do i declare the $row and $column am not sure about the testfile aswell Quote Link to comment https://forums.phpfreaks.com/topic/181299-writing-array-content-to-text-file/#findComment-956425 Share on other sites More sharing options...
mikesta707 Posted November 12, 2009 Share Posted November 12, 2009 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) Quote Link to comment https://forums.phpfreaks.com/topic/181299-writing-array-content-to-text-file/#findComment-956427 Share on other sites More sharing options...
sasa Posted November 12, 2009 Share Posted November 12, 2009 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"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/181299-writing-array-content-to-text-file/#findComment-956435 Share on other sites More sharing options...
davocold Posted November 12, 2009 Author Share Posted November 12, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/181299-writing-array-content-to-text-file/#findComment-956448 Share on other sites More sharing options...
mikesta707 Posted November 12, 2009 Share Posted November 12, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/181299-writing-array-content-to-text-file/#findComment-956452 Share on other sites More sharing options...
davocold Posted November 12, 2009 Author Share Posted November 12, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/181299-writing-array-content-to-text-file/#findComment-956461 Share on other sites More sharing options...
mikesta707 Posted November 12, 2009 Share Posted November 12, 2009 if its just the values, than a nested foreach loop should do the trick. alternatively you could just concatenate all the values into a string, or use implode, IE $text = implode(',', $array); Quote Link to comment https://forums.phpfreaks.com/topic/181299-writing-array-content-to-text-file/#findComment-956465 Share on other sites More sharing options...
davocold Posted November 12, 2009 Author Share Posted November 12, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/181299-writing-array-content-to-text-file/#findComment-956515 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.