Jump to content

writing and reading an array to a file


mmaczollek

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/250259-writing-and-reading-an-array-to-a-file/
Share on other sites

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. >_<

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.