Convert an array to JSON data with PHP

November 03, 2012 // In: PHP, Web Design

php json encodeIts the age of ajax, jquery, json etc. You often need to convert your PHP variable/array to JSON string. In PHP json_encode function converts PHP variable to JSON string which then can be used in javascript, jquery, ajax etc.

Lets suppose we have a 2D array with the name Employee having some data

It could be static array or fetched from database. If you fetch it from database let suppose our array looks like this

As we have supposed our array name is $Employee so we need to encode and echo as JSON using json_encode() function

and complete code looks like

and resulting JSON encoded string looks like

Load JSON Data with jQuery and PHP
Add Facebook Comments to Your WordPress themes