note 109953 added to function.serialize

From: Date: Wed, 05 Sep 2012 09:42:04 +0000
Subject: note 109953 added to function.serialize
Groups: php.notes 
Request: Send a blank email to php-notes+get-190512@lists.php.net to get a copy of this message
When you serialize an array the internal pointer will not be preserved. Apparently this is the expected behavior but was a bit of a gotcha moment for me. Copy and paste example below. //Internal Pointer will be 2 once variables have been assigned. $array = array(); $array[] = 1; $array[] = 2; $array[] = 3; //Unset variables. Internal pointer will still be at 2. unset($array[0]); unset($array[1]); unset($array[2]); //Serialize $serializeArray = serialize($array); //Unserialize $array = unserialize($serializeArray); //Add a new element to the array //If the internal pointer was preserved, the new array key should be 3. //Instead the internal pointer has been reset, and the new array key is 0. $array[] = 4; //Expected Key - 3 //Actual Key - 0 echo "<pre>" , print_r($array, 1) , "</pre>"; ---- Server IP: 69.147.83.197 Probable Submitter: 78.143.213.243 ---- Manual Page -- http://www.php.net/manual/en/function.serialize.php Edit -- https://master.php.net/note/edit/109953 Del: integrated -- https://master.php.net/note/delete/109953/integrated Del: useless -- https://master.php.net/note/delete/109953/useless Del: bad code -- https://master.php.net/note/delete/109953/bad+code Del: spam -- https://master.php.net/note/delete/109953/spam Del: non-english -- https://master.php.net/note/delete/109953/non-english Del: in docs -- https://master.php.net/note/delete/109953/in+docs Del: other reasons-- https://master.php.net/note/delete/109953 Reject -- https://master.php.net/note/reject/109953 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#190512) next »