Search results
function add_quotes($str) { return sprintf("'%s'", $str); } $csv = implode(',', array_map('add_quotes', $array)); DEMO. Also note that there is fputcsv if you want to write to a file.
You can use array_walk to iterate all the elements in side the array passing the reference to the element and add the quotes in the following way. php 7.4 or newer <?php $arr = ['a','b','c']; array_walk($arr, fn(&$x) => $x = "'$x'"); echo implode(',', $arr); // 'a','b','c' php 7.3 or older version
implode (PHP 4, PHP 5, PHP 7, PHP 8) implode — Join array elements with a string
The implode () function returns a string from the elements of an array. Note: The implode () function accept its parameters in either order. However, for consistency with explode (), you should use the documented order of arguments. Note: The separator parameter of implode () is optional.
How it works. First, define two arrays of strings, one for columns and the other for excluding columns. Second, use the array_filter () function to filter the columns in the excluded list. Third, use the implode () function to join the strings in the array returned by the array_filter () function.
Our article is about the PHP function implode(), which is used to join array elements with a string. This function is useful for working with arrays and strings in PHP. In this article, we will discuss the syntax and usage of implode(), as well as provide some examples.
7 lis 2023 · The PHP implode function essentially takes an array and converts it into a string. It does this by joining the array elements with a string that you specify. The function accepts two parameters: the "glue" string that will be placed between the array elements and the array itself.