note 57921 deleted from ref.array by levim
| From: | levim@php.net | Date: | Tue, 06 May 2014 22:02:30 +0000 |
| Subject: | note 57921 deleted from ref.array by levim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-199448@lists.php.net to get a copy of this message | ||
Note Submitter: msajko at gmail dot com
----
array_to_string and sister function string_to_array with multi dimensional array support.
// Converts an array to a string that is safe to pass via a URL
function array_to_string($array) {
$retval = '';
$null_value = "^^^";
foreach ($array as $index => $val) {
if(gettype($val)=='array') $value='^^array^'.array_to_string($val); else
$value=$val;
if (!$value)
$value = $null_value;
$retval .= urlencode(base64_encode($index)) . '|' .
urlencode(base64_encode($value)) . '||';
}
return urlencode(substr($retval, 0, -2));
}
// Converts a string created by array_to_string() back into an array.
function string_to_array($string) {
$retval = array();
$string = urldecode($string);
$tmp_array = explode('||', $string);
$null_value = urlencode(base64_encode("^^^"));
foreach ($tmp_array as $tmp_val) {
list($index, $value) = explode('|', $tmp_val);
$decoded_index = base64_decode(urldecode($index));
if($value != $null_value){
$val= base64_decode(urldecode($value));
if(substr($val,0,8)=='^^array^') $val=string_to_array(substr($val,8));
$retval[$decoded_index]=$val;
}
else
$retval[$decoded_index] = NULL;
}
return $retval;
}