note 103717 added to function.serialize

From: Date: Fri, 29 Apr 2011 15:03:57 +0000
Subject: note 103717 added to function.serialize
Groups: php.notes 
Request: Send a blank email to php-notes+get-179275@lists.php.net to get a copy of this message
Hello, I wanted to serialize a complex array (an array of arrays with a few levels, it's a configuration file) and I think I have found a bug in the 'serialize' function. I work with PHP 5.2.5 (from SLES 10). I had as result always the string "N;". This value corresponds to NULL value. So I did my own 'serialize' function. Here it is in case you had the same problem. <?php # Function which returns serialization string for a scalar value private function ser_value($value) { if (is_null($value)) { return "N"; } elseif (is_bool($value)) { return $value ? "b:0":"b:1"; } elseif (is_integer($value)) { return "i:".$value; } else { return "s:".strlen($value).":\"".$value."\""; } } # Recursive function which returns serialization string for an array function serialize_data($array) { $n = count($array); $result = "a:".$n.":{"; $i = 1; foreach ($array as $key => $value) { $result .= ser_value($key).";"; $result .= (is_array($value)) ? serialize_data($value) : ser_value($value).";"; } $result .= "}"; return $result; } ?> Greetings from Spain José M. Ciges ---- Server IP: 195.221.21.36 Probable Submitter: 194.250.98.243 ---- Manual Page -- http://www.php.net/manual/en/function.serialize.php Edit -- https://master.php.net/note/edit/103717 Del: integrated -- https://master.php.net/note/delete/103717/integrated Del: useless -- https://master.php.net/note/delete/103717/useless Del: bad code -- https://master.php.net/note/delete/103717/bad+code Del: spam -- https://master.php.net/note/delete/103717/spam Del: non-english -- https://master.php.net/note/delete/103717/non-english Del: in docs -- https://master.php.net/note/delete/103717/in+docs Del: other reasons-- https://master.php.net/note/delete/103717 Reject -- https://master.php.net/note/reject/103717 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#179275) next »