2

I have following array

$type=Array(
[0] => PL
[1] => LWP
[2] => Carry Forward
[3] => SL
);

I want to convert it into String.I used implode function to convert it.

$newarray=implode(",", $type);

but it reurn string like below

PL,LWP,Carry Forward,SL

I want String like below

("PL","LWP","Carry Forward","SL")

Please help me...

2
  • 1
    well, for starters, you want quotes inbetween your values instead of just commas - have you tried using a glue for implode that has quotes? Commented Feb 7, 2017 at 10:35
  • 1
    $newString = "(" . implode("\",\"", $type) . ")"; Commented Feb 7, 2017 at 10:36

6 Answers 6

7

Try this one ;)

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

Comments

2

As you want double quotes in the string generated,you can replace the glue of implode with implode('", "', $type) instead of implode(", ", $type) These double quotes will not appear in the 1st and last value of string.. So add (" in the beginning and ") at the end of implode. See the code below for entire syntax

<?php
    $type=Array(
    [0] => PL
    [1] => LWP
    [2] => Carry Forward
    [3] => SL
    );
    //Convert array to string with double quotes for each value
    $newarray='("'.implode('", "', $type).'")';
    echo $newarray;
    ?>

1 Comment

@TobySpeight Edited..Thanks
1

You can try with "," glue and adding prefix (" and postfix ")

$newarray= '("' . implode('","', $type) . '")';

You should also think about edge case when you have empty array since you may want to handle it slightly different. In this case it would result ("")

Comments

0

As it stands, your question is unanswerable. You are trying to create a machine parseable representation of data without providing nearly enough information about how this is to be parsed.

e.g. What if one the elements already contains a double quote - you need to escape it. But which method do you use for escaping?

e.g. Quoting a numerical value may then affect its interpretation by the consumer of the data (although your example does not include numerical data).

You might consider using fputcsv(); (but this will only write to a stream - so if you need the data elsewhere then you'd need to capture the stream.

Comments

0

The current answers disregard the edge case of $type being empty by returning ("") instead of () which is probably what you want.

You may want to surround each element by quotes before adding the parentheses like so:

$type = array("PL", "LWP", "Carry Forward", "SL");
$quoted = array_map( function($element) { return "\"${element}\""; }, $type );
$newarray = '(' . implode(',', $quoted) . ')';

Comments

0
sprintf("'%s'", implode("','", $type))

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.