infoCopter

home | newsletter | site map
Free Stuff
: links
: downloads
: Neue Surf Tips
: know-how
: Zitate
: HTML Checker



Activities
--in spare time

: vorträge



About IC
: friends
: lust und frust
: guestbook
: what's new

[ know-how ] - [ my php ] - serialize


See also
Printable VersionPrinter-friendly

string serialize (mixed value) - [ unserialize() ]

<html>
<h2>string serialize (mixed value)</h2>

<?

$hash = array (
	foo => "bar" , 
	goo => "car" 
);

// print '<h3>' . $hash['foo'] . '</h3>'; // prints 'bar' ;-)

///// serialize
$sh = serialize ($hash);

print $sh . '<br> <br>';

///// unserialize
$backhash = unserialize($sh);

var_dump($backhash);

print '<br> <br>';

while (list($key, $value) = each ($backhash)) {
	print "<b>$key</b> = «$value» <br>\n";
}

?>
</html>

Output

string serialize (mixed value)
a:2:{s:3:"foo";s:3:"bar";s:3:"goo";s:3:"car";}

array(2) { ["foo"]=> string(3) "bar" ["goo"]=> string(3) "car" } 

foo = «bar» 
goo = «car» 



Unserialize from a serialized file

serialize.dat:
a:2:{s:3:"foo";s:3:"bar";s:3:"goo";s:3:"car";}

<html>
<h2>string serialize (mixed value)</h2>

<?

///// unserialize
$backhash = unserialize(getFile('serialize.dat'));

var_dump($backhash);

print '<br> <br>';

while (list($key, $value) = each ($backhash)) {
	print "<b>$key</b> = «$value» <br>\n";
}

function getFile($fname, $legacy=false) {
        $tmp = "";

        if ($legacy) {
            $modes = "rt";
        } else {
            $modes = "rb";
        }

        if ($fd = fopen($fname, $modes)) {
                while (!feof($fd)) {
                        $tmp .= fread($fd, 65536);
                }
                fclose($fd);
        }
        return $tmp;
}

?>
</html>

Output

string serialize (mixed value)
array(2) { ["foo"]=> string(3) "bar" ["goo"]=> string(3) "car" } 

foo = «bar» 
goo = «car» 

[Blue Ribbon Campaign icon]
Join the Blue Ribbon Online Free Speech Campaign!

©1998-2002 infoCopter