note 69240 added to ref.array
| From: | ob at babcom dot biz | Date: | Mon, 28 Aug 2006 12:56:00 +0000 |
| Subject: | note 69240 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-116428@lists.php.net to get a copy of this message | ||
Here are two functions Array2String() and String2Array() based on functions posted below by daenders
AT yahoo DOT com.
An improvement handling NULL values correctly was posted by designatevoid at gmail dot com.
My version also solves the NULL-value-problem plus keeps support of multidimensional arrays.
<?php
// convert a multidimensional array to url save and encoded string
// usage: string Array2String( array Array )
function Array2String($Array) {
$Return='';
$NullValue="^^^";
foreach ($Array as $Key => $Value) {
if(is_array($Value))
$ReturnValue='^^array^'.Array2String($Value);
else
$ReturnValue=(strlen($Value)>0)?$Value:$NullValue;
$Return.=urlencode(base64_encode($Key)) . '|' .
urlencode(base64_encode($ReturnValue)).'||';
}
return urlencode(substr($Return,0,-2));
}
?>
<?php
// convert a string generated with Array2String() back to the original (multidimensional) array
// usage: array String2Array ( string String)
function String2Array($String) {
$Return=array();
$String=urldecode($String);
$TempArray=explode('||',$String);
$NullValue=urlencode(base64_encode("^^^"));
foreach ($TempArray as $TempValue) {
list($Key,$Value)=explode('|',$TempValue);
$DecodedKey=base64_decode(urldecode($Key));
if($Value!=$NullValue) {
$ReturnValue=base64_decode(urldecode($Value));
if(substr($ReturnValue,0,8)=='^^array^')
$ReturnValue=String2Array(substr($ReturnValue,8));
$Return[$DecodedKey]=$ReturnValue;
}
else
$Return[$DecodedKey]=NULL;
}
return $Return;
}
?>
----
Server IP: 66.163.161.117
Probable Submitter: 84.185.247.44
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/ref.array.php
Edit -- https://master.php.net/note/edit/69240
Del: integrated -- https://master.php.net/note/delete/69240/integrated
Del: useless -- https://master.php.net/note/delete/69240/useless
Del: bad code -- https://master.php.net/note/delete/69240/bad+code
Del: spam -- https://master.php.net/note/delete/69240/spam
Del: non-english -- https://master.php.net/note/delete/69240/non-english
Del: in docs -- https://master.php.net/note/delete/69240/in+docs
Del: other reasons-- https://master.php.net/note/delete/69240
Reject -- https://master.php.net/note/reject/69240
Search -- https://master.php.net/manage/user-notes.php