Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.