Articles

PHP Array to JSON: How to use PHP json_encode()

Through this tutorial, 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.

To convert PHP array to JSON, use the json_encode() function. The json_encode() is a built-in PHP function that converts an array to json. The json_encode() function converts PHP-supported data type into JSON formatted string to be returned due to JSON encode operation.

PHP json_encode: Convert Array To JSON Object, Array Object To JSON Object

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

Syntax of PHP json_encode() function

json_encode(value, options);

Parameters of PHP json_encode() function

ParametersTypeDescription
valueMixedAny PHP type except resource. Must be UTF character encoded data.
optionsIntegerBitmask 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

 'test', 'email' => '[email protected]', 'mobile' => '88888xxxx', 'age' => 25);
echo json_encode($arr)."\n";

?>

Output of the above code is:- {“name”:”test”,”email”:”[email protected]”,”mobile”:”88888xxxx”,”age”:25}

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:

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.

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.

 'Test', 
            'Age' => 24, 
            'Email' => '[email protected]'); 
echo json_encode($arr)."\n";

?>

Output of the above code is: {“Name”:”Test”,”Age”:24,”Email”:”[email protected]”}

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:

 array(
    'product_id' => 1,
    'product_color' => 'Black',
    'product_name' => 'Laptap',
    'brand_name' => 'DELL',
  )
);

echo json_encode($data)."\n";

?>

Output of the above code is: {“product”:{“product_id”:1,”product_color”:”Black”,”product_name”:”Laptap”,”brand_name”:”DELL”}}

Recommended PHP Tutorials

  1. Functions: Remove First Character From String PHP
  2. Remove Specific/Special Characters From String In PHP
  3. How to Replace First and Last Character From String PHP
  4. Reverse String in PHP
  5. Array Push, POP PHP | PHP Array Tutorial
  6. PHP Search Multidimensional Array By key, value and return key
  7. remove duplicates from multidimensional array PHP
  8. PHP Remove Duplicate Elements or Values from Array PHP

Images mentioned above related to PHP are either copyright property of respective image owners.

Rabins Sharma Lamichhane

Rabins Sharma Lamichhane is the owner of RabinsXP who is constantly working for increasing the Internet of Things (IoT) in Nepal. He also builds android apps and crafts beautiful websites. He is also working with various social services. The main aim of Lamichhane is to digitally empower the citizens of Nepal and make the world spiritually sound better both in terms of technology and personal development. Rabins is also the first initiator of Digital Nepal.

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button