0

Given an array:

$arr = array('item1', 'item2', 'item3');

How can I implode this into a string that looks like:

'item1', 'item2', 'item3'

Note the single quotes in above output are important. I realise I can do this using a foreach but am wondering if there is a more elegant way similar to implode and join that can do this in a single function. I can only seem to implode the array into a CSV list without the single quotes:

echo implode(',', $arr);
//outputs
item1, item2, item3
0

1 Answer 1

5

Add the quotes to the implode call:

$Csv = "'" . implode("', '", $arr) . "'";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.