How to convert array to json in PHP?

This post will give you example of How to convert array to json in php.

1. Code:

<?php
function convertArrayToJson($array){
	$data = json_encode($array);
    return $data;
} 
   $array = [
	  [ "id" => 1, "name" => "Hardik Savani", "email" => "hardik@gmail.com"],
	  [ "id" => 2, "name" => "Jaydeep Pather", "email" => "jaydeep@gmail.com"],
	  [ "id" => 3, "name" => "Vivek Pather", "email" => "vivek@gmail.com"]
   ];
      print_r(convertArrayToJson($array));
?>

2. Example:

Input:

$array = [
	  [ "id" => 1, "name" => "Hardik Savani", "email" => "soltuts@gmail.com"],
	  [ "id" => 2, "name" => "Jaydeep Pather", "email" => "soltuts@gmail.com"],
	  [ "id" => 3, "name" => "Vivek Pather", "email" => "soltuts@gmail.com"]
   ];

Output:

[
   {
      "id": 1,
      "name": "Hardik Savani",
      "email": "soltuts@gmail.com"
   },
   {
      "id": 2,
      "name": "Jaydeep Pather",
      "email": "soltuts@gmail.com"
   },
   {
      "id": 3,
      "name": "Vivek Pather",
      "email": "soltuts@gmail.com"
   }
]