note 53172 deleted from function.array-map by philip
| From: | philip@php.net | Date: | Thu, 22 Feb 2007 02:01:35 +0000 |
| Subject: | note 53172 deleted from function.array-map by philip | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-123691@lists.php.net to get a copy of this message | ||
Note Submitter: ildar-sh [at] mail [dot] ru
----
I think that thise code can be useful for an string<->assoc.array convertation
<?php
/*
// Example:
$origin = array('a' => 1, 'b' => 2, 'c' => 3);
$joined = PrepareRequest::join($origin);
$splitted = PrepareRequest::split($joined);
print_r(array('origin' => $origin, 'joined' => $joined,
'splitted' => $splitted));
*/
class PrepareRequest
{
/*
* string PrepareRequest::join(array $array [ , char $delim1 [ , char $delim2 ] ] )
*
* Converting of arrays to the strings. Resulting string contains keys and values
* correctly for HTTP requests such as key1=value1&key2=value2...
*
*/
function join($array, $delim1='&', $delim2='=')
{
array_walk($array, array('PrepareRequest', '_join'), $delim2);
return implode($delim1, $array);
}
function _join(&$value, $key, $delim2)
{
$value = $key . $delim2 . $value;
}
/*
* array PrepareRequest::split(string $string [ , char $delim1 [ , char $delim2 ] ] )
*
* Parsing of string to associative array
*/
function split($string, $delim1='&', $delim2='=')
{
$array = explode($delim1, $string);
$result = array();
for ($i = 0; $i < count($array); $i++)
{
list($key, $value) = explode($delim2, $array[$i], 2);
$result[$key] = $value;
}
return $result;
}
}
?>