note 93313 modified in function.serialize by danbrown

From: Date: Wed, 02 Sep 2009 21:53:20 +0000
Subject: note 93313 modified in function.serialize by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-160182@lists.php.net to get a copy of this message
NOTE: php's serialize does not properly serialize arrays with which a slice of the array is a reference to the array itself, observe: <?php $a = array(); $a[0] = "blah"; $a[1] =& $a; $a[1][0] = "pleh"; // $a[0] === "pleh" $b = unserialize(serialize($a)); // $b[0] == "pleh", $b[1][0] == "pleh" $b[1][0] = "blah"; ?> now $b[1][0] == "blah", but $b[0] == "pleh" after serializing and unserializing, slice 1 is no longer a reference to the array itself... I have found no way around this problem... even manually modifying the serialized string from 'a:2:{i:0;s:4:"pleh";i:1;a:2:{i:0;s:4:"pleh";i:1;R:3;}}' to 'a:2:{i:0;s:4:"pleh";i:1;R:1;}' to force the second slice to be a reference to the first element of the serialization (the array itself), it seemed to work at first glance, but then unreferences it when you alter it again, observe: <?php $testser = 'a:2:{i:0;s:4:"pleh";i:1;R:1;}'; $tmp = unserialize($testser); print_r($tmp); print "\n-----------------------\n"; $tmp[1][0] = "blah"; print_r($tmp); ?> outputs: Array ( [0] => pleh [1] => Array *RECURSION* ) ----------------------- Array ( [0] => pleh [1] => Array ( [0] => blah [1] => Array ( [0] => pleh [1] => Array *RECURSION* ) ) ) --was-- NOTE: php's serialize does not properly serialize arrays with which a slice of the array is a reference to the array itself, observe: [code] $a = array(); $a[0] = "blah"; $a[1] =& $a; $a[1][0] = "pleh"; // $a[0] === "pleh" $b = unserialize(serialize($a)); // $b[0] == "pleh", $b[1][0] == "pleh" $b[1][0] = "blah"; [/code] now $b[1][0] == "blah", but $b[0] == "pleh" after serializing and unserializing, slice 1 is no longer a reference to the array itself... I have found no way around this problem... even manually modifying the serialized string from 'a:2:{i:0;s:4:"pleh";i:1;a:2:{i:0;s:4:"pleh";i:1;R:3;}}' to 'a:2:{i:0;s:4:"pleh";i:1;R:1;}' to force the second slice to be a reference to the first element of the serialization (the array itself), it seemed to work at first glance, but then unreferences it when you alter it again, observe: [code] $testser = 'a:2:{i:0;s:4:"pleh";i:1;R:1;}'; $tmp = unserialize($testser); print_r($tmp); print "\n-----------------------\n"; $tmp[1][0] = "blah"; print_r($tmp); [/code] outputs: Array ( [0] => pleh [1] => Array *RECURSION* ) ----------------------- Array ( [0] => pleh [1] => Array ( [0] => blah [1] => Array ( [0] => pleh [1] => Array *RECURSION* ) ) ) http://php.net/manual/en/function.serialize.php

« previous php.notes (#160182) next »