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

<?php
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 data from a model on all pages in Laravel – Simple Way

Do you know How to get data from a model on all pages in Laravel ? In this blog post, […]

August 5, 2022

Export html table to excel, pdf, csv format using Datatable

Do you know how to export html table to excel, pdf, CSV, or excel format using Datatable? If you don’t […]

August 4, 2022

How to create a Laravel Collection From JSON

Today i have to create a laravel collection from a JSON object. I search on the internet and I cant […]

August 3, 2022

Instagram Hashtag API PHP and JavaScript Integration with RapidAPI

Do you know how to integrate Instagram Hashtag API with your website? In this tutorial, I will show you how […]

August 2, 2022

Digital Marketing Toolkit

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

Get Free Access Now