$data = array( 'name' => 'John Doe', 'age' => 35, 'address' => array( 'street' => '123 Main St', 'city' => 'Anytown', 'state' => 'CA' ) ); $json = json_encode($data); echo $json;
class Person { public $name; public $age; public $address; function __construct($name, $age, $address) { $this->name = $name; $this->age = $age; $this->address = $address; } } $person = new Person('John Doe', 35, array('street' => '123 Main St', 'city' => 'Anytown', 'state' => 'CA')); $json = json_encode($person); echo $json;In this example, we have a PHP class called Person that contains information about a person's name, age, and address. We create a new instance of this class and then use the json_encode() function to convert the object into a JSON string. The PHP JSON encode function is part of the PHP standard library.