• Calculating...
  • 5 years ago
  • 7.4K Views
  • Archived This is an Archived post.
    The content within this may not be used or replaced with newer versions.

Convert PHP Array To JSON : Examples

This tutorial focuses on converting PHP Array to JSON. It is used to read data from the server and display it to the Web. JSON is a text format , and We can convert any JS Object into a JSON format.

 

  1. Covert PHP array to JSON

PHP is capable of handling JSON and it has some built-in functions to handle them. json_encode() is used to convert objects, arrays in PHP to JSON format. The PHP json_encode() function returns the string containing a JSON equivalent of the value passed.

 

The syntax of json_encode() function as following.

 

json_encode(value, options)

 

 

Here’s how we need to convert the numerically indexed array into JSON

<?php
$names = ['Bishrul', 'Bishrul Haq', 'Developer', 'Software Engineer'];
$namesJSON = json_encode($names);
echo "Names in JSON :".$namesJSON;
 

Output :

Names in JSON :["Bishrul","Bishrul Haq","Developer","Software Engineer"]
 

If you want the array to be output as Object , you can use JSON_FORCE_OBJECT option as below,

<?php
$names = ['Bishrul', 'Bishrul Haq', 'Developer', 'Software Engineer'];
$namesJSON = json_encode($names,JSON_FORCE_OBJECT);
echo "Names in JSON :".$namesJSON;
 

Output :

Names in JSON :{"0":"Bishrul","1":"Bishrul Haq","2":"Developer","3":"Software Engineer"}
 
  1. Convert PHP Associative Array to JSON

To Convert a key-value pair array (Associative Array), you can also use json_encode() to convert objects, arrays in PHP to JSON format.

Here’s how we need to convert an associative array into JSON

<?php
$names = ['Name'=>'Bishrul', 'Full Name'=>'Bishrul Haq', 'Details'=>'Developer', 'Designation'=>'Software Engineer'];
$namesJSON = json_encode($names);
echo "Names in JSON :".$namesJSON;
 

Output :

Names in JSON :{"Name":"Bishrul","Full Name":"Bishrul Haq","Details":"Developer","Designation":"Software Engineer"}
 

Hope this tutorial helped you! Feel free to drop your opinion at the comment section.

Share:

Related Post

Installing PHP on Windows

PHP is known to be one of the most commonly used server-side programming language on the web. It provides an easy to master with a simple learning curve. It has close ties with the MySQL database.

  • 3 years ago

Simple PHP Page Router

This is a simple yet basic PHP routing application created to direct all request to index.php and route the files to it’s relevant paths.

  • 5 years ago

Simple PHP MySQL CRUD Application

PHP is widely used by many developers today and it’s one of the easiest language to learn. This tutorial will help you to learn the basic CRUD (Create, Read, Update, Delete) operations.

  • 5 years ago

PHP Program to remove empty or specific array elements

This tutorial is created to remove specific or empty (NULL) array elements from the array.

  • 5 years ago

PHP Array Functions and their usage with examples

This tutorial is created to give an abstract idea about commonly used PHP array function with examples. Through this tutorial you’ll be able to know, how to use array functions to make code more short

  • 4 years ago