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.



Further readings
Ajax & PHP scripts
Creating XMLHttp object in different browsers
Sample Code: Get Method of Ajax form submission
Dependant drop down list box using Ajax & PHP
Email validation using Ajax in a form
Getting customer details by entering customer id using Ajax
Progress Bar using Ajax
Progress Bar using MySQL PHP & Ajax
Displaying Message at client side using Ajax & PHP
Web page HTML form validation using Ajax and PHP
Form validation with onBlur event using Ajax and PHP

Json support in PHP
json_encode to generate Json string from PHP Array data
Json Data formatting to return database records to main script
Searching MySql database as we type using Ajax
Displaying all records based on selection of a drop down list box

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
Ajax & PHP
PHP Sections