note 93313 added to function.serialize

From: Date: Wed, 02 Sep 2009 18:16:58 +0000
Subject: note 93313 added to function.serialize
Groups: php.notes 
Request: Send a blank email to php-notes+get-160168@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: [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* ) ) ) ---- Server IP: 69.147.83.197 Probable Submitter: 99.2.115.248 ---- Manual Page -- http://www.php.net/manual/en/function.serialize.php Edit -- https://master.php.net/note/edit/93313 Del: integrated -- https://master.php.net/note/delete/93313/integrated Del: useless -- https://master.php.net/note/delete/93313/useless Del: bad code -- https://master.php.net/note/delete/93313/bad+code Del: spam -- https://master.php.net/note/delete/93313/spam Del: non-english -- https://master.php.net/note/delete/93313/non-english Del: in docs -- https://master.php.net/note/delete/93313/in+docs Del: other reasons-- https://master.php.net/note/delete/93313 Reject -- https://master.php.net/note/reject/93313 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#160168) next »