SQL PHP HTML ASP JavaScript articles and free scripts to download

json_encode to convert data to string

Json is a data exchange format like XML. You can learn about Json support of PHP here. We will learn about php function json_encode() here. This function is built in higher versions of PHP. To know about support of your PHP version regarding Json you can check the Json Introduction page.

json_encode function takes data and converts them to a string, we call it as Json string. This string format follows a standard so it can be decoded back to its original data form at the receiving end.

Let us start with an array of data. Here is an simple data array in PHP, we will apply json_encode() to this array.

$a=array("Apple"=>10,"Orange"=>40,"Mango"=>80,"Banana"=>30);
echo json_encode($a);

The output of the above code is here.

{"Apple":10,"Orange":40,"Mango":80,"Banana":30}

This is one of the simple requirements, so we will try some thing bit complex than this by using two arrays.

The output of the above code is here.

[{"Apple":10,"Orange":40,"Mango":80,"Banana":30},{"d":4,"e":5,"f":6}]

Note the square bracket at the outside. The two set of data are separated by a coma and included inside curl bracket.

You can start from here making your json string to exchange data across pages.
Google+
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked



Join Our Email List
Email:  
For Email Newsletters you can trust
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer