$data = array( "name" => "John", "age" => 30, "city" => "New York" ); $json = json_encode($data); echo $json;
{"name":"John","age":30,"city":"New York"}
class Person { public $name; public $age; public $city; } $person = new Person; $person->name = "John"; $person->age = 30; $person->city = "New York"; $json = json_encode($person); echo $json;
{"name":"John","age":30,"city":"New York"}Package library: php G json_encode is part of the PHP standard library and does not require any external package or library.