While you’re all here, the topic continues: convert the array to a variable

Folk, is there an advanced version of the implode function for associative arrays?

For example:

$cars = array('van'=>'volvo xc70','coupe'=>'mercedes clk gtr','suv'=>'nissan pathfinder'); $glue1 = '<br />'; $glue2 = ' - '; extended_implode($glue1,$glue2,$cars); 


 van - volvo xc70<br /> coupe - mercedes clk gtr<br /> suv - nissan pathfinder<br /> 

NB: If there is no regular function, I personally do not need an algorithm. =) So in this case, the question changes to "what mistakes are in my method".

 function mb_extended_implode($glue1,$glue2,$elements,$enc='utf-8'){ $out = ''; foreach($elements as $key=>$value){ $out .= $key.$glue2.$value.$glue1; } $out = mb_substr($out,0,mb_strlen($glue2,$enc),$enc); return $out; } 
  • Good question. - AseN

4 answers 4

 $res = array_map(function($k, $v) { return "$k - $v"; }, array_keys($cars), $cars); echo implode('<br />', $res); 

Or like this, depending on whether you need <br /> at the end or not:

 $res = array_map(function($k, $v) { return "$k - $v<br />"; }, array_keys($cars), $cars); echo implode('', $res); 
  • Good way. Check mark. Approximately three times faster than mine. =) - knes


But this will not work?

  • Generally, very close. So much so that I even put a plus sign and a tick. Let's just say: this is a clearer and simpler way, even considering that you may have to get rid of the URL-encode. - knes
  • just a plus sign: after all, there is a lot of trouble with the result: spaces are cut, equal signs are put ... But the function itself is very useful. - knes 1:09 pm

Here's what you can find on php.net :

  Here is a function to implode and array including the key and value pair. <?php /** * 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 ); } ?> You can, for example, encode an array to be sent as an URL query using this: <?php $query = url_encode( array_implode( '=', '&', $array ) ); ?> Or if you want to output an HTML element attributes: <?php echo '<input '.array_implode( '="', '" ', $array ).' />'; ?> Hope it can help someone! 
  • one
    Well, I do not see, frankly, significant differences from my method. - knes
  • Yes, there are probably no special ones, it is just from the site of the Pkhp manual - this is probably the advanced version of the ala "official" function - Chad

The version is good, but not complete. It is enough to add recursion, and then a fully multidimensional array will be parsed.

 $val = array_implode( $glue, $separator, $val ); 

Here is the whole function. Here we will have both keys and values.

 function array_implode( $glue, $separator, $array ) { if ( ! is_array( $array ) ) return $array; $string = array(); foreach ( $array as $key => $val ) { if ( is_array( $val ) ) $val = array_implode( $glue, $separator, $val ); $string[] = "{$key}{$glue}{$val}"; } return implode( $separator, $string ); }