note 96118 added to function.serialize
| From: | darlanatrdacom dot com dot br at osu1 dot php dot net | Date: | Tue, 09 Feb 2010 18:54:40 +0000 |
| Subject: | note 96118 added to function.serialize | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-165803@lists.php.net to get a copy of this message | ||
I've made a simple code to serialize small arrays.. may be useful if someone needs to read the
data in JS or store the array in database in a simple format. base64_encode/decode functions in JS
can be found over the web...
<?php
function array2str($array,$level=1) {
$str = array();
foreach($array as $key=>$value) {
$nkey = base64_encode($key);
$nvalue = is_array($value)?'$'.base64_encode(array2str($value)) :
(string)base64_encode($value);
$str[] = $nkey.'&'.$nvalue;
//printf("Encoded %s,%s to %s,%s\n",$key,$value,$nkey,$nvalue);
}
return implode('|',$str);
}
function str2array($str) {
$rest = array();
if(strpos($str,'|')>0) {
$array = explode('|',$str);
} else $array=array($str);
foreach($array as $token) {
list($key,$value) = explode('&',$token);
$nkey=base64_decode($key);
$nvalue = ( substr($value,0,1) != '$' ? base64_decode($value) :
str2array(base64_decode(substr($value,1))) );
$rest[$nkey] = $nvalue;
//printf("Decoded %s,%s to %s,%s\n",$key,$value,$nkey,$nvalue);
}
return $rest;
}
?>
[]'s
----
Server IP: 69.147.83.197
Probable Submitter: 201.41.46.217
----
Manual Page -- http://www.php.net/manual/en/function.serialize.php
Edit -- https://master.php.net/note/edit/96118
Del: integrated -- https://master.php.net/note/delete/96118/integrated
Del: useless -- https://master.php.net/note/delete/96118/useless
Del: bad code -- https://master.php.net/note/delete/96118/bad+code
Del: spam -- https://master.php.net/note/delete/96118/spam
Del: non-english -- https://master.php.net/note/delete/96118/non-english
Del: in docs -- https://master.php.net/note/delete/96118/in+docs
Del: other reasons-- https://master.php.net/note/delete/96118
Reject -- https://master.php.net/note/reject/96118
Search -- https://master.php.net/manage/user-notes.php