note 106084 deleted from function.implode by tularis
| From: | tularis@php.net | Date: | Sun, 09 Oct 2011 09:33:41 +0000 |
| Subject: | note 106084 deleted from function.implode by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-183750@lists.php.net to get a copy of this message | ||
Note Submitter: lightningspirit at gmail dot com
----
Here is a function to implode and array including the key and value pair.
[code]
/**
* Implode an array with the key and value pair giving
* a glue, a separator between pairs and the array
* to implode.
* @param string $glue The glue between key and value
* @param string $separator Separator between pairs
* @param array $array The array to implode
* @return string The imploded array
*/
function array_implode( $glue, $separator, $array ) {
if ( ! is_array( $array ) ) return $array;
$string = array();
foreach ( $array as $key => $val ) {
if ( is_array( $val ) )
$val = implode( ',', $val );
$string[] = "{$key}{$glue}{$val}";
}
return implode( $separator, $string );
}
[/code]
You can, for example, encode an array to be sent as an URL query using this:
<code>
$query = url_encode( array_implode( '=', '&', $array ) );
</code>
Or if you want to output an HTML element attributes:
<code>
echo '<input '.array_implode( '="', '" ', $array ).'
/>';
</code>
Hope it can help someone!