PHP json_encode: Convert Array To JSON, Object To JSON

The PHP json_encode() function is used to convert PHP array or object into JSON object. PHP has some pre inbuilt functions to handle JSON.

we will learn how to convert PHP Object to JSON, convert PHP String to JSON, PHP Array To JSON, get data from JSON array in php convert Multidimensional PHP Array into JSON with definition, syntax, and examples.

JSON means JavaScript object notation. This is the data saved in the .json file and consists of a series of key/value pairs format. JSON is used to transfer data between server-side and client-side.

PHP json_encode: Convert Array To JSON, Object To JSON

Defination: The PHP json_encode() function is used to convert PHP array or object into JSON.

Syntax

json_encode(value, options);

Parameters of PHP json_encode() function

Parameters 
value
options	 
Type	
 Mixed  
 Integer

 Description
Any PHP type except 
                                      resource. Must be UTF 
                                      character encoded data.
  Bitmask comprising of 
                                      JSON_HEX_QUOT,
                                      JSON_HEX_TAG, 
                                      JSON_HEX_AMP, 
                                      JSON_HEX_APOS,                                     
                                      JSON_NUMERIC_CHECK,
                                      JSON_PRETTY_PRINT, 
                                      JSON_UNESCAPED_SLASHES, 
                                      JSON_FORCE_OBJECT.

Example 1 – PHP JSON Encode

<?php
 
$arr = array('name' => 'test', 'email' => 'test@gmail.com', 'mobile' => '99999xxxx', 'age' => 27);
echo json_encode($arr)."\n";
 
?>

Output of the above code is:-

{“name”:”test”,”email”:”test@gmail.com”,”mobile”:”99999xxxx”,”age”:27}

Example 2 – Convert Object to JSON in PHP

Here, we will convert Object to JSON format using PHP json_encode() method. Let’s see example below:

<?php
 
class Color {
   
}
 
$color = new Color();
$color->title = 'Block';
$color->code = 'FFF';
 
$json = json_encode($color);
echo $json."\n"; 
 
?>

Output of the above code is:

{“title”:”Block”,”code”:”FFF”}

Example 3 – Convert String to JSON in PHP

Here we will take another example using the PHP json_encode() function.

<?php
 
$string = "hello php dev";
echo json_encode($string)."\n";
 
?>

The output of the above code is:

“hello php dev”

Example 4 – PHP Convert Array To JSON

How to get data from JSON array in PHP?

We will take an example for convert PHP array to JSON or get data from JSON array in PHP using the PHP json_encode() function.

<?php
 
$arr = array('Name' => 'Test', 
            'Age' => 25, 
            'Email' => 'test@gmail.com'); 
echo json_encode($arr)."\n";
 
?>

Output of the above code is:

 {“Name”:”Test”,”Age”:25,”Email”:”test@gmail.com”}

Example 5- PHP Convert Multidimensional Array into JSON

If you want to convert multidemsional array to json in PHP using the json_encode() function. Let’s see the example below:

<?php
 
$data = array(
  'product' => array(
    'product_id' => 1,
    'product_color' => 'Black',
    'product_name' => 'Laptap',
    'brand_name' => 'LENOVO',
  )
);
 
echo json_encode($data)."\n";
 
?>

Output of the above code is:

{“product”:{“product_id”:1,”product_color”:”Black”,”product_name”:”Laptap”,”brand_name”:”LENOVO”}}

Leave a Reply

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