PHP Basics

How can we use JSON in PHP Scripting?

JSON in PHP – JSON is not object, not an array or any kind of datastructure. People often misunderstood it. JSON is serialized data. JSON is useful to send data in key value format.

JSON – Javascript Object Notation

How to use JSON in PHP?

Lets understand the usage of json in php files.

Example

<?php

$fruits=array(“orange”,”apple”,”grape”);

?>

In this example, we have defined an array $fruits which holds some fruit names. To convert this into an array, we have to use PHP function json_encode.

Syntax

json_encode(array);

Example

<?php

$fruits=array(“orange”,”apple”,”grape”);

$jsonData=json_encode($fruits);

var_dump($jsonData); // var_dump php function prints whats inside the json variable.

?>