note 57785 deleted from ref.array by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 14:54:13 +0000 |
| Subject: | note 57785 deleted from ref.array by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-197216@lists.php.net to get a copy of this message | ||
Note Submitter: designatevoid at gmail dot com
----
Here's an improvement to the array_to_string and string_to_array functions posted by daenders
AT yahoo DOT com above.
They now handle NULL values correctly.
<?php
// 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 => $value) {
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)
$retval[$decoded_index] = base64_decode(urldecode($value));
else
$retval[$decoded_index] = NULL;
}
return $retval;
}
?>