Add to Favorites

php json_encode and json_decode

JSON is used in many places on the web. It got it's start with ajax and has trickled into use in many other places. It is being used a lot now with web API's and I have even seen it make it's way into being used as a simple way to store serialized objects.

PHP has recognized the importance of JSON in today's web, with later versions of php the json_encode and json_decode functions are included in the core build. In versions earlier than 5.2 you may need to install the extension for these functions.

These two functions make it easy to move JSON data around. Lets say you have an array containing associative arrays with data intended to be used in a select box. You want to return this data for an ajax call so the javascript can build out the dom elements for a call. In the past you may have generated the json by hand or used a third party library, but things are easier than ever with these great functions. It's as simple as:

echo json_encode($my_array);


Be the first to leave a comment on this post.

Leave a comment

To leave a comment, please log in / sign up