note 43817 deleted from ref.array by tomsommer
| From: | tomsommer@php.net | Date: | Mon, 05 Jul 2004 06:57:47 +0000 |
| Subject: | note 43817 deleted from ref.array by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-72563@lists.php.net to get a copy of this message | ||
Note Submitter: john at mccarthy dot net
----
I needed a function to create a string from a variable list. EVen if the list contained multiple
arrays of arrays. The string also needed to not have the \n character, so I could save it as a
string (even if the data contained a \n).
Here are the two functions I made:
function variables_encode($ar)
{
if (!is_array($ar))
{
$r = rtrim(base64_encode($ar), "=");
}
else
{
foreach ($ar as $key => $value)
{
if ($r != '') { $r .= "&"; }
$r .= rtrim(base64_encode($key), "=");
$r .= "|";
$r .= rtrim(base64_encode(variables_encode($value)), "=");
}
}
return $r;
}
function variables_decode($str)
{
if (!strstr($str,"&"))
{
$r = base64_decode($str);
}
else
{
$ar = explode("&",$str);
foreach ($ar as $ls)
{
list($key,$value) = explode("|",$ls);
$r[base64_decode($key)] = variables_decode(base64_decode($value));
}
}
return $r;
}
// And an example:
$object = "hp_contentbox.php";
$data = array("title"=>"Title","content"=>"Stuff
goes here");
$objectlist[] = array("object"=>$object,"data"=>$data);
$object = "hp_vspacer.php";
$objectlist[] = $object;
$object = "hp_contentbox.php";
$data = array("title"=>"Profile","content"=>"User
stuff");
$objectlist[] = array("object"=>$object,"data"=>$data);
$r = variables_encode($objectlist);
$o2 = variables_decode($r);
print_r($objectlist);
echo "encoded: " . $r . "\n";
print_r($o2);