mmaczollek Posted November 1, 2011 Share Posted November 1, 2011 so im trying to write and read an array to a file. i can get it to write to the file, but when i try to echo it on my page it only says "Array". i also can't edit the text file on my file manager, but when i download it i can see that its been written correctly. here is the code im using that i found on a forum... does anybody have some ideas as to why this isn't working? <?php $filename ="phplay/writearray.txt"; function save_array_to_file($filename,$b) { if (!is_resource($filename)) { if (!$file = fopen($filename,'w+')) return false; } else { $file = $filename; } foreach ($b as $key=>$val) { fwrite($file,(is_int($key) ? chr(6).(string)$key : chr(5).$key)); if (is_array($val)) { fwrite($file,chr(0)); //array starts save_array_to_file($file,$val); fwrite($file,chr(1)); //array ends } elseif (is_int($val)) { fwrite($file,chr(2).(string) $val); //int } elseif (is_string($val)) { fwrite($file,chr(3).$val); //string } } if (!is_resource($filename)) fclose($file); return true; } function read_array_from_file($filename) { if (!is_resource($filename)) { if (!$file = fopen($filename,'r')) return false; } else { $file = $filename; } $ret=array(); $key=''; $val=null; $mod=0; while (!feof($file)) { $b = fread($file,1); if (ord($b) < 9) { if ($val!=null) { if ($mod==2) $val=(int) $val; if ($mod==3) $val=(string) $val; $ret[$key]=$val; $key=''; $val=null; $mod=0; } else { if (ord($b)==0) $mod=0; elseif (ord($b)==1) return $ret; else { if ($mod==5) $key=(string) $key; if ($mod==6) $key=(int) $key; $mod=ord($b); } } } else { if ($mod==5 || $mod==5) $key.=$b; elseif ($mod==0) $val=read_array_from_file($file); else $val.=$b; } } if (!is_resource($filename)) fclose($file); return $ret; } ?> <?php $employee_array[0] = "Bob"; $employee_array[1] = "Sally"; $employee_array[2] = "Charlie"; $employee_array[3] = "Clare"; $employee_array[4] = "Matt"; save_array_to_file($filename,$employee_array) ?> <?php $thearray = read_array_from_file($filename); echo "$thearray"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/250259-writing-and-reading-an-array-to-a-file/ Share on other sites More sharing options...
manohoo Posted November 2, 2011 Share Posted November 2, 2011 deleted my post, i realized it wasn't what you were looking for Quote Link to comment https://forums.phpfreaks.com/topic/250259-writing-and-reading-an-array-to-a-file/#findComment-1284124 Share on other sites More sharing options...
trq Posted November 2, 2011 Share Posted November 2, 2011 i can get it to write to the file, but when i try to echo it on my page it only says "Array". That is because you can't echo an array. Quote Link to comment https://forums.phpfreaks.com/topic/250259-writing-and-reading-an-array-to-a-file/#findComment-1284144 Share on other sites More sharing options...
mmaczollek Posted November 2, 2011 Author Share Posted November 2, 2011 well i changed it to print_r, but it still isn't working correctly. it shows the first entry and thats it. are there any good threads w/ this explained already? ive done some searching and couldn't find much. i feel stupid... but its been a while since i've programmed. >_< Quote Link to comment https://forums.phpfreaks.com/topic/250259-writing-and-reading-an-array-to-a-file/#findComment-1284149 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.