note 54870 deleted from function.serialize by aharvey
| From: | aharvey@php.net | Date: | Tue, 06 Nov 2012 02:08:05 +0000 |
| Subject: | note 54870 deleted from function.serialize by aharvey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-191759@lists.php.net to get a copy of this message | ||
Note Submitter: mwwaygoo AT hotmail DOT com
----
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).