Re: add value to serialized array?
| From: | Bas Jobsen | Date: | Tue, 05 Feb 2002 17:32:01 +0000 |
| Subject: | Re: add value to serialized array? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-83611@lists.php.net to get a copy of this message | ||
Hello,
OKay, when i replace the last rows with:
$fp=fopen("test.txt","r");
flock($fp,1);
$array=array_merge($array,unserialize(fread($fp,filesize("test.txt"))));
flock($fp,3);
fclose($fp);
it works.
But now i want to place this in a class, and it goes wrong again.
Hope you can help, thanks,
Bas
--
<?
class test
{
function test(){;}
function read()
{
$array=array();
$fp=fopen("test.txt","r");
flock($fp,1);
$array=unserialize(fread($fp,filesize("test.txt")));
flock($fp,3);
fclose($fp);
return $array;
}
function save($array)
{
$fp=fopen("test.txt","w+");
flock($fp,2);
fwrite($fp,serialize($array));
flock($fp,3);
fclose($fp);
}
function push($array,$value)
{
array_push($array,$value);
$this->save($array);
}
}
$test= new test;
$array=$test->read();
echo $array[0].$array[1].$array[2];
$test->push($array,"three");
//$test->save($array);
$array=$test->read();
echo $array[0].$array[1].$array[2];
?>
--
Op dinsdag 05 februari 2002 12:24, schreef u:
> Hi,
>
> At start test.txt contains a serialized array with value "one" and "two".
> a:2:{i:0;s:3:"one";i:1;s:3:"two";}
> after one run this is:
> a:3:{i:0;s:3:"one";i:1;s:3:"two";i:2;s:4:"tree";}
>
> But the last row prints nothing. Why?
>
> Tnx,
>
> Bas
>
> <?
> $array=array();
> $fp=fopen("test.txt","r");
> flock($fp,1);
> $array=unserialize(fread($fp,filesize("test.txt")));
> flock($fp,3);
> fclose($fp);
>
> array_push($array,"tree");
>
> $fp=fopen("test.txt","w+");
> flock($fp,2);
> fwrite($fp,serialize($array));
> flock($fp,3);
> fclose($fp);
>
> $fp=fopen("test.txt","r");
> flock($fp,1);
> $array=unserialize(fread($fp,filesize("test.txt")));
> flock($fp,3);
> fclose($fp);
>
> echo $array[0].$array[1].$array[2];
> ?>