10

I have a string, values are in brackets and separated with comma, like array string:

Example:

[[["Name" , "ID"] , [12]] , ["Test" , 78] , 0]

How to convert this string to PHP array?

1

1 Answer 1

25

That's a JSON string representation of an array, use json_decode():

$array = json_decode($string);    
print_r($array);

Yields:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => Name
                    [1] => ID
                )
            [1] => Array
                (
                    [0] => 12
                )
        )
    [1] => Array
        (
            [0] => Test
            [1] => 78
        )
    [2] => 0
)

If it had any { } that would be an object and decoded as a stdClass object in PHP unless you pass true to json_decode() to force an array.

Since it's structured as a PHP array (as of PHP 5.4), this works as well (don't use eval on untrusted data). There is absolutely no reason to do this, it's just illustrative:

eval("\$array = $string;");
print_r($array);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.