How to split an array into single quotes and comma separated list in PHP
How to split an array into single quotes and comma separated list in PHP, When we are working dynamic SQL queries we have to build them using the post array, after splitting the array field key and values into separate array’s. we need to have single quote mark arround the values and it is separated with comma in sql INSERT statement like this.
INSERT INTO table_name (column1, column2, column3) VALUES (‘value1’, ‘value2’, ‘value3’).
Our goal is to achieve the highlighted text from the array, for this we are using implode function, appending single quote mark and comma followed by a space and othe single quote mark.( ‘, ‘ ). If we only use implode function it will insert the between the values, first quote mark and the last quote mark will be missed, for that we are appeding single quote mark at the string starting and the ending. Then we will ge the output what we wanted.[php] <?php
$array = array(‘hi’, ‘this’, ‘is’, ‘an’, ‘array’);
$comma_list = "’" .implode("’, ‘", $array) . "’";