note 43817 added to ref.array
| From: | johnatmccarthydotnet at rn2 dot php dot net | Date: | Mon, 05 Jul 2004 06:05:45 +0000 |
| Subject: | note 43817 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-72558@lists.php.net to get a copy of this message | ||
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);
----
Manual Page -- http://www.php.net/manual/en/ref.array.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+43817
Delete -- http://master.php.net/manage/user-notes.php?action=delete+43817&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+43817&report=yes
Search -- http://master.php.net/manage/user-notes.php