scs-php Posted July 10, 2008 Share Posted July 10, 2008 Hello, I am a total newbie to PHP and could use some help. ON Javascript side, before submitting data via Ajax, I use: myData.toJSONString(); // where myData is a simple array On the Server side, I end up with something like this: $jsondata = '[{"one":"1","two":"2","three":"3"}]'; Then I use json_decode: $myArray = json_decode($jsondata,true); When I try to access the array data, it is blank?????? echo $myArray["two"]; // returns blanks However, if above $jsondata does not contain [ and ] Example: $jsondata = '{"one":"1","two":"2","three":"3"}'; Then it works: echo $myArray["two"]; // outputs: 2 Questions: Why does toJSONString return the array inside []? I assume I should not be stripping the brackets to make my code work, but I am at a loss on how to access my array values. Please Help. Thanks, one frustrated newbie. Note: I am using PHP ver 5.2.6 Link to comment https://forums.phpfreaks.com/topic/114050-solved-tojsonstring-and-array-access/ Share on other sites More sharing options...
rajivgonsalves Posted July 10, 2008 Share Posted July 10, 2008 first of all I would like the ask what is the value of the javascript myData in the first place secondly its a object in a object you could access it with your above by using the following echo $myArray[0]->two; hope its helpful Link to comment https://forums.phpfreaks.com/topic/114050-solved-tojsonstring-and-array-access/#findComment-586208 Share on other sites More sharing options...
scs-php Posted July 10, 2008 Author Share Posted July 10, 2008 Ahh... so the brackets turn the array into an object. I thought that may be the case but am such a newbie to php that I did not know the proper syntax to access the data via the object. Thanks for the help Wiz..... Link to comment https://forums.phpfreaks.com/topic/114050-solved-tojsonstring-and-array-access/#findComment-586243 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.