note 57785 added to ref.array

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

« previous php.notes (#96991) next »