Smash
Smash

Reputation: 513

How to make an array of this string?

I have a string like this:

$str = '"list":["test 1","test 2"]';

How to make an array:

$arr['list'] = array('test 1','test 2');

?

Upvotes: 0

Views: 108

Answers (1)

anubhava
anubhava

Reputation: 786329

Make it a valid JSON string and use json_decode function like this:

$str = '"list":["test 1","test 2"]';
$arr = json_decode('{' . $str . '}', true);
print_r($arr);

OUTPUT:

Array
(
    [list] => Array
        (
            [0] => test 1
            [1] => test 2
        )
)

Upvotes: 3

Related Questions