Return JSON with PHP
When bringing Ajax technologies into your websites, it’s almost certain you’ll comes across the need to return data from the backend in a format that JavaScript works well with, such as JSON. With that, PHP is often a popular choice amongst developers for backend programming due to its wide adoption and low cost (FREE!). Thankfully PHP is actively developed and it supports many of todays web standards including JSON.
Since PHP 5.2, there are a couple of functions to help you consume PHP into the backend as well as return it to your frontend application for an XHR call. We’ll just look at the return portion since that is what you’ll be using most commonly. For this, PHP provides you with json_encode() which will return a JSON formatted String of a value you pass to it. The value can be any type except a resource. For instance, lets say you’ve just selected a few rows out of a database and you have an array of values. Below, we’ll define the array manually so you have a visual idea of what this looks like:
$arr = array(); $arr[] = "foo"; $arr[] = "bar"; $arr[] = "baz";
To have PHP output this as a JSON string, simply use json_encode($arr) as below:
$arr = array(); $arr[] = "foo"; $arr[] = "bar"; $arr[] = "baz"; echo json_encode($arr)
Check out the output of the above code. As you can see it returns a basic JSON array with the values in it:
["foo","bar","baz"]
If you wanted to return a JSON object, you could use PHP arrays as well. PHP arrays allow you to use Strings as the index. In many languages, this is known as an associative array and it looks like the code below:
$arr = array( "foo" => "bar", "baz" => "true", "thinger" => "thing" );
To put this to practice, say you are returning a simple saved shopping cart to your user, your data might look as follows:
<?php $arr = array( "cartId" => 456186, "lastModified" => "Tue, 19 Jan 2010 03:14:07 GMT", "items" => array( 15642, 45616, 54984, 45751 ) ); echo json_encode($arr); ?>
Here we have a cartId, the date it was lastModified, and an array of itemIds. Obviously if you were returning this to the frontend, you would probably want the items to be more descriptive so you don’t have to make separate calls for each id to get the product info. This is just a demo so it will serve our purposes just fine.
Check out the output of the above code snippet.
Below is what it looks like as well:
{ "cartId": 456186, "lastModified": "Tue, 19 Jan 2010 03:14:07 GMT", "items": [15642, 45616, 54984, 45751] }
You can see that PHP gave us a JSON object at the highest level (denoted by the beginning and ending curly braces). Each property of the object was the associative array properties that we defined in the PHP code. Finally, you’ll notice that since we provided an array of ids for items, PHP returned a JSON array with the ids.
Hopefully you can see the value in this. By providing a native JavaScript format of data to our frontend functions, you’ll have a much easier time working with this data when you use any of the Dojo XHR functions such as dojo.xhrGet().




