note 57921 added to ref.array
| From: | msajkoatgmaildotcom at osu1 dot php dot net | Date: | Wed, 19 Oct 2005 08:25:23 +0000 |
| Subject: | note 57921 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-97212@lists.php.net to get a copy of this message | ||
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;
}
----
Manual Page -- http://www.php.net/manual/en/ref.array.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+57921
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57921&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57921&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57921&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57921&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57921&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57921&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57921&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+57921&report=yes
Search -- http://master.php.net/manage/user-notes.php