json_encode() | How To Convert PHP’s Associative Array to JSON Data using PHP

Date Published: 24/03/2020 Published By: JaiSchool

The json_encode() is a function of PHP. It is used to transform the associative array of PHP into JSON data (Javascript object notation).

Create an associative array to change it into JSON data. Here I have created an array which is:-

$assoc_array = array("name"=>"Jaischool","city"=>"Sikar","state"=>"RJ","country"=>"India");

To transform $assoc_array into JSON data, use json_encode() function and give associative array name in its parenthesis.

$json_data = json_encode($assoc_array);

Now print it with print_r() function. Or you can also print it by an echo.

echo $json_data;

json_encode() function's code example

<?php

$assoc_array=array("name"=>"Jaischool","city"=>"Sikar","state"=>"RJ","country"=>"India");
	
$json_data = json_encode($assoc_array);

echo $json_data

?>
Related article
JSON data to PHP object
implode() function in PHP
Print an array in PHP
Get length of string in PHP

Publish A Comment

Leave a Reply

Your email address will not be published. Required fields are marked *