Jump to content

[SOLVED] toJSONString() and Array access


scs-php

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.