I have an array that looks like this:
array('first' => 'value1', 'second' => 'value2', ...);
How can I get all the keys and put them in a comma separated string?
At the very end I need something like this, to do a query:
values IN ('first','second',...)
Thanks
To convert an array to a comma-separated string, call the join() method on the array, passing it a string containing a comma as a parameter. The join method returns a string containing all array elements joined by the provided separator. Copied!
The comma separated list can be created by using implode() function. The implode() is a builtin function in PHP and is used to join the elements of an array. implode() is an alias for PHP | join() function and works exactly same as that of join() function.
Use the string join() function to get the comma separated strings.
How to Convert a Python List into a Comma-Separated String? You can use the . join string method to convert a list into a string. So again, the syntax is [seperator].
should be enough:
echo "'".implode("','", array_keys($array))."'";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With