1

I am trying to decode json into array in PHP.But My json is like

string(307) " string(290) "{"id":"1","name":"test name","rowno":"0","note":"test notes","created_date":"2016-05-01","updated_date":"2016-05-12 05:08:05"}" "

string within a string!! How to convert it into array.

2
  • This is a var_dump() of the json. Where is this json being produced? Show the code that generates it. Commented May 13, 2016 at 3:53
  • 1
    Where are getting the string(307) " string(290) from? Commented May 13, 2016 at 3:53

1 Answer 1

3

This looks like you're trying to decode (or trying to output) the data with var_dump(). That isn't the function you require; what you require is json_decode():

$data = json_decode($json);

If that isn't the issue, and you're actually receiving the data as above then you'll have to strip it out - most likely using a regex like the following:

$s = 'string(307) " string(290) "{"id":"1","name":"test name","rowno":"0","note":"test notes","created_date":"2016-05-01","updated_date":"2016-05-12 05:08:05"}" "';

preg_match('/\{(.*)\}/', $s, $matches);

print_r($matches);

Which would return your json:

Array
(
    [0] => {"id":"1","name":"test name","rowno":"0","note":"test notes","created_date":"2016-05-01","updated_date":"2016-05-12 05:08:05"}
    [1] => "id":"1","name":"test name","rowno":"0","note":"test notes","created_date":"2016-05-01","updated_date":"2016-05-12 05:08:05"
)

Thus allowing you to decode it properly within $matches.

Regex is a beast to me so I'll try explain as best as possible what the expression is doing:

  • \{ matches the first open {
  • (.*) matches any character inbetween
  • \} matches the closing }
Sign up to request clarification or add additional context in comments.

2 Comments

Yes you are right.The problem is am using var_dump() instead of echo.I changed it and it is working fine.Thank you :-)
After a lot RND, this is the answer which work for me and making sans.

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.