Often times when coding in PHP, we have the need to turn an array into a comma separated string, or even an object into a comma separated string. As a Florida PHP Developer, I’d like to save you some time and show you the code.
Simple PHP array to comma separated string
$myArray = array(‘this’, ‘that’); $str = join(‘, ‘, $myArray); echo $str;
That’s the basic way of doing it. But what if you have an associated array?
Associated array to comma separated string
function to_string($data, $glue=’, ‘) { $output = ”; if(!empty($data) && count($data) > 0) { $values