Jump to content

Recommended Posts

I am using Javascript to encode data with json... and using PHP to decode this data with json_decode();

 

My JSON looks like this:

 

 

When I do json_decode, it doesn't work

 

The reason why, is because of the "0":, "1": etc

 

Usually, json is not encoded with {"0":{"url":"http://www.businessinsider.com

 

but rather, just {"url":"http://www.businessinsider.com

 

without the "{0":

 

What is your solution for this??

 

Can PHP decode the "{0":

 

Or, is the solution to strip the "{0": altogether...? If so, how would I do this?

 

Thanks

Edited by KingNeil
Link to comment
https://forums.phpfreaks.com/topic/300754-help-with-json_decode/
Share on other sites

The "0": and "1": are there because you encoded an array-like object. Such json is perfectly valid, and would decode just fine with json_decode. You'll probably want to pass the $assoc flag as true so it decodes to an array rather than an object, that will make accessing things easier.

 

$array = json_decode($json, true);
var_dump($array[0], $array[1]);

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.