0

If I have a example string like this: "dataset1"=>"blank","gdataset"=>"f1,f2"

I'm trying to create an array of the key/value pairs. Desired array result should look like this:

Array(
 [0] => "dataset1"=>"blank"
 [1] => "gdataset"=>"f1,f2"
)

I've tried http_build_query & explode w/o success, as the array key or value is getting mangled.

What should I use to get the desired array?

2
  • 1
    Just curious, why wouldn't you want: Array( "dataset1"=>"blank", "gdataset"=>"f1,f2" ) Commented May 13, 2019 at 20:30
  • Oh, I had to think about what you were asking, but I need to keep the key/value pairs together as a string, as I will potentially write them back into a file, exactly as you see them here. Commented May 13, 2019 at 20:40

1 Answer 1

1

It's kind of hackish, but just replace "," with "","" to keep the quotes and then explode on ",":

$result = explode('","', str_replace('","', '"",""', $string));

But I'm positive that there is a much better way to do whatever you are doing in general. Maybe a new question outlining why you are doing this?

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I knew there must of been a relatively easy way to do this ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.