Hazzdood Hazzdood - 1 year ago 99
PHP Question

php json_decode not working on json array

I have been searching the internet for hours now, and i can't figure it out.

$json = '{"pages":[{"name": "Page1","inputs":[{"title": "Catagory","name": "catagory","type": "radio","options":[{"name": "Paper","value": "paper"}{"name": "Letter","value": "letter"}]}{"title": "Title","name": "title","type": "text"}{"title": "File","name": "file","type": "file","fileName": "?pages[0].inputs[0]"}{"title": "Submit","type": "submit"}]}]}';

$result = json_decode($json, true);
echo $result['pages'][0]['name'];
echo $pages[0]['name'];

Im just simply trying to parse some json but the website says this:

Notice: Undefined variable: pages in C:\Users\hazzj\Desktop\Stuff\Apache-Server\htdocs\WMS\Author\submit\test.php on line 7

Answer Source

You'd missed out commas between strings {}. Use this modified $json variable:

$json = '{"pages":[{"name": "Page1","inputs":[{"title": "Catagory","name": "catagory","type": "radio","options":[{"name": "Paper","value": "paper"},{"name": "Letter","value": "letter"}]},{"title": "Title","name": "title","type": "text"},{"title": "File","name": "file","type": "file","fileName": "?pages[0].inputs[0]"},{"title": "Submit","type": "submit"}]}]}';

$result = json_decode($json, true);
echo $result['pages'][0]['name'];    // Output: Page1
echo $pages[0]['name'];           // Not sure what this $pages variable is
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download