note 54870 added to function.serialize

From: Date: Mon, 18 Jul 2005 15:03:12 +0000
Subject: note 54870 added to function.serialize
Groups: php.notes 
Request: Send a blank email to php-notes+get-92115@lists.php.net to get a copy of this message
Hello people, I had major problems getting serialize to work properly, as I wanted to sync two databases via email, and needed a way of converting a retrieved row to a string. It works the reverse of array(), in that it creates a statement that you can use to evaluate to the original array, so I called it unarray(). Its a recursive function (self calling), so it doesnt matter how complex the array is. <?php $source=array(1,4,'x'=>'test'); $text=unarray($source); $destination=array(); // prime the variable incase of failure. eval('$destination='.$text); print_r($destination); // unarray.php REVERSE of array(); function unarray($array, $first=TRUE) { $text=""; if(is_array($array)) { $text.="array("; while(list($key,$value)=each($array)) { if(is_string($key)) { $text.="'".$key."'"; } else { $text.=$key; } $text.="=>"; $text.=unarray($array[$key], FALSE); } $text=$text."), "; } else { if(is_string($array)) { $text.="'".addslashes($array)."', "; } else { if(is_bool($array)) { if($array) { $text.="TRUE, "; } else { $text.="FALSE, "; } } else { if(is_null($array)) { $text.="NULL, "; } else { $text.=$array.", "; } } } } if($first) $text=substr($text,0,-2).";"; return $text; } ?> p.s. I realise that it also creates unnecessary commas, but these are ignored when re-creating it. array(1,2,) is the same as array(1,2). ---- Manual Page -- http://www.php.net/manual/en/function.serialize.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+54870 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54870&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54870&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54870&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54870&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54870&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54870&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+54870&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#92115) next »