Google News
logo
PHP Program to Decode JSON data into a PHP object
In the following example of PHP program to decode JSON data into a PHP object :
Program :
<?php
// JSON data to decode
$json_data = '{"name":"John Smith","age":30,"city":"New York"}';

// Decode JSON data into a PHP object
$php_object = json_decode($json_data);

// Access object properties
echo "Name: " . $php_object->name . "<br>";
echo "Age: " . $php_object->age . "<br>";
echo "City: " . $php_object->city . "<br>";
?>
Output :
Name: John Smith
Age: 30
City: New York
In the above program, we first define a JSON string that we want to decode. We then use the `json_decode()` function to decode the JSON data into a PHP object. We can then access the object properties using the object operator `->`.