Tutorial by Examples

The json_decode() function takes a JSON-encoded string as its first parameter and parses it into a PHP variable. Normally, json_decode() will return an object of \stdClass if the top level item in the JSON object is a dictionary or an indexed array if the JSON object is an array. It will also retur...
The json_encode function will convert a PHP array (or, since PHP 5.4, an object which implements the JsonSerializable interface) to a JSON-encoded string. It returns a JSON-encoded string on success or FALSE on failure. $array = [ 'name' => 'Jeff', 'age' => 20, 'active' => ...
When json_encode or json_decode fails to parse the string provided, it will return false. PHP itself will not raise any errors or warnings when this happens, the onus is on the user to use the json_last_error() and json_last_error_msg() functions to check if an error occurred and act accordingly in ...
PHP 5.x5.4 When you build REST API's, you may need to reduce the information of an object to be passed to the client application. For this purpose, this example illustrates how to use the JsonSerialiazble interface. In this example, the class User actually extends a DB model object of a hypotetica...
By adding a header with content type as JSON: <?php $result = array('menu1' => 'home', 'menu2' => 'code php', 'menu3' => 'about'); //return the json response : header('Content-Type: application/json'); // <-- header declaration echo json_encode($result, true); // <--- e...

Page 1 of 1