note 106084 added to function.implode

From: Date: Sat, 08 Oct 2011 16:58:53 +0000
Subject: note 106084 added to function.implode
Groups: php.notes 
Request: Send a blank email to php-notes+get-183727@lists.php.net to get a copy of this message
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! ---- Server IP: 69.147.83.197 Probable Submitter: 89.180.160.204 ---- Manual Page -- http://www.php.net/manual/en/function.implode.php Edit -- https://master.php.net/note/edit/106084 Del: integrated -- https://master.php.net/note/delete/106084/integrated Del: useless -- https://master.php.net/note/delete/106084/useless Del: bad code -- https://master.php.net/note/delete/106084/bad+code Del: spam -- https://master.php.net/note/delete/106084/spam Del: non-english -- https://master.php.net/note/delete/106084/non-english Del: in docs -- https://master.php.net/note/delete/106084/in+docs Del: other reasons-- https://master.php.net/note/delete/106084 Reject -- https://master.php.net/note/reject/106084 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#183727) next »