json_encode() Function | Convert PHP Array to JSON

In this tutorial i will discuss about php json_encode() inbuilt method. json_encode — Returns the JSON representation of a value. In php json_encode() function is an inbuilt function in PHP which is used to convert PHP array or object into JSON representation.

Syntax :

string json_encode( $value, $option, $depth )

 

Parameters:

  • $value: It is a mandatory parameter which defines the value to be encoded.
  • $option: It is optional parameter which defines the Bitmask consisting of JSON_FORCE_OBJECTJSON_HEX_QUOTJSON_HEX_TAG, etc. The behaviour of these constants is described on the JSON constants page
  • $depth: It is optional parameter which sets the maximum depth. Its value must be greater than zero.

 

// Declare an array  
$value = array( 
    "name"=>"CodeChief", 
    "email"=>"[email protected]"); 
   
// Use json_encode() function 
$json = json_encode($value); 
   
// Display the output 
echo($json); 

 

Output:

json_encode
{"name":"CodeChief","email":"[email protected]"}

 

Reference: https://www.php.net/manual/en/function.json-encode.php

Hope it can help you.

 

A web enthusiastic, self-motivated Full-Stack Web Developer from Dhaka, Bangladesh with experience in developing applications using JavaScript, Laravel & Wordpress specifically. Facebook Github Website