1

I've tried searching google and nothing is clear, plus you guys are way faster and correct.

I have an array as follows:

var bannertext = new Array();
bannertext[1] = "ladidadi";
bannertext[2] = "blahblahblahblah";
bannertext[3] = "oooaaaaooooaaa";

How do I turn the bannertext array into a JSON encoded array so that I can send it to a PHP page and break it down using the json_decode function, and then how do I access the individual variables?

EDIT: Thanks to Gazler for that first part! My PHP page looks like this:

$bannertext = $_GET['bannertext'];
$banner = json_decode($bannertext);

How do I access each of those strings now? LIke echo $banner[1]; and so on?

asked Dec 8, 2011 at 20:03

2 Answers 2

2

You use JSON.stringify() however in IE you will need to include the json2 library as IE has no native JSON parsing.

var bannertext = new Array();
bannertext[1] = "ladidadi";
bannertext[2] = "blahblahblahblah";
bannertext[3] = "oooaaaaooooaaa";
console.log(JSON.stringify(bannertext));

As an aside, you can instantiate an array using the array literal syntax.

var bannertext = [];

Here is a blog post explaining the difference.

answered Dec 8, 2011 at 20:06
Sign up to request clarification or add additional context in comments.

1 Comment

IE 8+ brings native support for it; but still more-or-less true.
1

JSON.stringify:

JSON.stringify(bannertext);

Older browsers, IE7 and below, require an external library Json2 Free CDN

answered Dec 8, 2011 at 20:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.