note 39716 modified in function.explode by aidan
| From: | aidan@php.net | Date: | Sun, 27 Jun 2004 14:03:45 +0000 |
| Subject: | note 39716 modified in function.explode by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-71907@lists.php.net to get a copy of this message | ||
I needed a solution to implode and explode associative arrays. Now I use these two functions (there
may be a better solution, but it works ;) ):
<?php
/**
* @name implodeAssoc($glue,$arr)
* @description makes a string from an assiciative array
* @parameter glue: the string to glue the parts of the array with
* @parameter arr: array to implode
*/
function implodeAssoc($glue,$arr)
{
$keys=array_keys($arr);
$values=array_values($arr);
return(implode($glue,$keys).$glue.implode($glue,$values));
};
/**
* @name explodeAssoc($glue,$arr)
* @description makes an assiciative array from a string
* @parameter glue: the string to glue the parts of the array with
* @parameter arr: array to explode
*/
function explodeAssoc($glue,$str)
{
$arr=explode($glue,$str);
$size=count($arr);
for ($i=0; $i < $size/2; $i++)
$out[$arr[$i]]=$arr[$i+($size/2)];
return($out);
};
?>
--was--
I needed a solution to im- and explode associative arrays. Now I use these two functions (there may
be a better solution, but it works ;) ):
/**
**name implodeAssoc($glue,$arr)
**description makes a string from an assiciative array
**parameter glue: the string to glue the parts of the array with
**parameter arr: array to implode
**/
function implodeAssoc($glue,$arr)
{
$keys=array_keys($arr);
$values=array_values($arr);
return(implode($glue,$keys).$glue.implode($glue,$values));
};
/**
**name explodeAssoc($glue,$arr)
**description makes an assiciative array from a string
**parameter glue: the string to glue the parts of the array with
**parameter arr: array to explode
**/
function explodeAssoc($glue,$str)
{
$arr=explode($glue,$str);
$size=count($arr);
for ($i=0; $i < $size/2; $i++)
$out[$arr[$i]]=$arr[$i+($size/2)];
return($out);
};
http://php.net/manual/en/function.explode.php