Php using unserialize() and serialize() changing values in a saved file

Posted by Doodle on Stack Overflow See other posts from Stack Overflow or by Doodle
Published on 2010-04-21T20:06:21Z Indexed on 2010/04/21 20:23 UTC
Read the original article Hit count: 344

Filed under:
|
|

I have a serialized array of values saved to a file and need to change the value of one of the variables. In the example I change the value of $two and then save the whole array back into the file with the new value.

Is there a more efficient way of altering just the single value with out having to read and write the entire file/array.

$data = file_get_contents('./userInfo');

$data = unserialize($data);
extract($data);
$two="this is a altered value";

 $userData = array(
        'one'          => $one,
        'two'          => $two,
        'three'        => $three
        );

 $file=fopen("../userInfo",'w');
        fwrite($file, $userData);
 fclose($file);

© Stack Overflow or respective owner

Related posts about php

Related posts about serialize