How to convert a PHP array into JSON

Do you know How to convert a PHP array into JSON,  I will explain step by step in this tutorial.

If you are working with small frontend applications and you may not use a high-level framework like Laravel.  so that time you can use the help of this.

So I will explain How to Convert PHP array to JSON.

At the top of your PHP script, you have to add a header that mentions this is JSON.

header('Content-Type: application/json');

How lets create an array

$array = array();
$array['status'] = "true";
$array['name'] = "John";
$array['email'] = "[email protected]";

Convert this array to a JSON

echo json_encode($array,JSON_PRETTY_PRINT);


Now lets Look into the Example Source Code

Convert a PHP array into JSON Example

header('Content-Type: application/json');

$array = array(); 
$array['status'] = "true"; 
$array['name'] = "John"; 
$array['email'] = "[email protected]";

echo  json_encode($array,JSON_PRETTY_PRINT);


That’s all. Hope you like this

Check out more awesome PHP Tutorials

Post Your Questions on our forum

Post a question on Forum

Ajith Jojo Joseph

Self taught, dedicated young entrepreneur with many licensed products under his sleeve. Passionate about technology, business and excellence in general.

Share with your friends:

Comments are closed.

How to get the next value of an array and loop it in the array in PHP

Do you know How to get the next value of an array and loop it in the array in PHP […]

July 28, 2023

New Open Source CRM for project Management and Invoicing

I’m excited to announce the launch of my new open source project: Gmax CRM. an invoicing and project management tool […]

December 31, 2022

Post View Counts WordPress plugin (Documentation)

The Post View Counts plugin is a simple tool for tracking and displaying the number of views for each post […]

December 30, 2022

How to create Laravel Flash Messages

Laravel flash messages are a convenient way to display one-time notifications to the user after a form submission or other […]

December 22, 2022

Digital Marketing Toolkit

Get Free Access to Digital Marketing Toolkit. You can use all our tools without any limits

Get Free Access Now