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

Ajith Jojo Joseph

Hire Me for Freelancing $30/Hour ,
Contact via email : [email protected]

Share with your friends:

Comments are closed.

Drag and Drop with Swapy and PHP

Hello , You might had issues with drag and drop options in your dashboard. here i found a new javascript […]

September 17, 2024

How To Generate A PDF from HTML in Laravel 11

Hello , I was trying to generate a PDF payment receipt for my SAAS application and when i search for […]

June 22, 2024

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

Digital Marketing Toolkit

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

Get Free Access Now