1

The DB I am working with has an array of strings stored as a text value, such as:

"[\"Radio\/CD\",\"TV\",\"Weight Gauge\"]"

(no idea why stored like this)

Anyway, I need to convert it into a regular array, such as:

Array ( [0] => Weight Gauge [1] => TV [2] => Radio/CD )

Because of the way its stored I can't do a regular php explode, eg:

<?php
    $input  = '"[\"Radio\\\/CD\",\"TV\",\"Weight Gauge\"]"';
    echo "Input string:<br>" . $input . "<br><br>";
    $output = explode(",", stripslashes($input));
    print_r($output);
?>

The resulting array ( not how I want it ):

Array ( [0] => "["Radio/CD" [1] => "TV" [2] => "Weight Gauge"]" )

Thanks

1 Answer 1

2

Looks like it's double JSON encoded.

use json_decode

$input  = '"[\"Radio\\\/CD\",\"TV\",\"Weight Gauge\"]"';
$output = json_decode(json_decode($input));
print_r($output);

this outputs:

Array
(
    [0] => Radio/CD
    [1] => TV
    [2] => Weight Gauge
)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, lol, I had tried a single json_decode...hadn't thought about doing a double.

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.