Google News
logo
PHP Program to Encode an associative array into a JSON object
To encode an associative array into a JSON object in PHP, you can use the `json_encode()` function. Here is an example program that demonstrates how to encode an associative array into a JSON object :
Program :
<?php

// Sample associative array
$data = array(
    'name' => 'John Doe',
    'age' => 30,
    'email' => 'johndoe@example.com'
);

// Encode the array into a JSON object
$json = json_encode($data);

// Output the JSON object
echo $json;
?>
Output :
{"name":"John Doe","age":30,"email":"johndoe@example.com"}
In this example, the `json_encode()` function is used to encode the associative array `$data` into a JSON object. The resulting JSON object is then stored in the variable `$json`. Finally, the program outputs the JSON object using the `echo` statement.

This JSON object represents the original associative array in a serialized format that can be easily transmitted over a network or stored in a file.