Last active
February 3, 2021 15:55
-
-
Save pablo-sg-pacheco/72ce47badf81949596a8cac0a4616950 to your computer and use it in GitHub Desktop.
WordPress - Convert array to string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* converts array to string. | |
* | |
* @param $arr | |
* @param array $args | |
* | |
* @return string | |
*/ | |
function convert_array_to_string( $arr, $args = array() ) { | |
$args = wp_parse_args( $args, array( | |
'glue' => ', ', | |
'item_template' => '{value}' // {key} and {value} allowed | |
) ); | |
$transformed_arr = array_map( function ( $key, $value ) use ( $args ) { | |
$item = str_replace( array( '{key}', '{value}' ), array( $key, $value ), $args['item_template'] ); | |
return $item; | |
}, array_keys( $arr ), $arr ); | |
return implode( $args['glue'], $transformed_arr ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment