• 0

[PHP] Strings with double quotes to array


Question

Hello, how can I convert a string with double quotes into an array? Is there a function for doing it?

for example

$string = "\"one\", \"two\"";

$array1 = array($string);

if i echo $array1[0] it will display "one", "two" and if I echo $array1[1] it will display nothing, what I want to do is to store the string to $array1 like this, $array1 = array("one","two") so that if I echo $array1[0] it will display one and if I echo $array1[1] it will display two.

How will I do it? Thank you very much, :)

4 answers to this question

Recommended Posts

  • 0

$string = "\"one\", \"two\"";

can be changed to...

$string = '"one", "two"';

then explode() by the comma, so all in all:

$string = '"one", "two"';
$array = explode(", ", $string);

You could keep $string as it was, but my way looks cleaner :) Simple, huh?

Note, that is to keep the quotes intact; if you just wanted the values inside the quotes:

$string = 'one, two';
$array = explode(", ", $string);

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.