$data = array( 'name' => 'John', 'age' => 25, 'gender' => 'male' ); $json = Zend_Json::encode($data); echo $json;
{"name":"John","age":25,"gender":"male"}
$options = Zend_Json::PRETTY_PRINT; $data = array( 'name' => 'John', 'age' => 25, 'gender' => 'male' ); $json = Zend_Json::encode($data, $options); echo $json;
{ "name": "John", "age": 25, "gender": "male" }
class Employee { public $name; public $age; public $gender; } $emp = new Employee(); $emp->name = 'John'; $emp->age = 25; $emp->gender = 'male'; $json = Zend_Json::encode($emp); echo $json;
{"name":"John","age":25,"gender":"male"}This example encodes a PHP object into a JSON string using the Zend_Json::encode() method. Zend_Json is a package/library in Zend Framework for encoding and decoding JSON data in PHP.