note 69240 deleted from ref.array by levim
| From: | levim@php.net | Date: | Tue, 06 May 2014 22:06:39 +0000 |
| Subject: | note 69240 deleted from ref.array by levim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-199459@lists.php.net to get a copy of this message | ||
Note Submitter: ob at babcom dot biz
----
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;
}
?>