PHP to JSON: Convert with json_encode()

In PHP, you can use the built-in function json_encode() to convert an array or object into JSON format. Here is an example code:

$data = array(
    'name' => 'John Doe',
    'age' => 30,
    'city' => 'New York'
);

$jsonData = json_encode($data);

echo $jsonData;

The code above converts the array $data into JSON format and outputs it. In practical development, different types of data can be converted to JSON format as needed, and then this format can be used for transmission or storage.

bannerAds