Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to create an array for JSON using PHP?

  • Last Updated : 03 Dec, 2021

In this article, we will see how to create an array for the JSON in PHP, & will see its implementation through examples.

Array: Arrays in PHP is a type of data structure that allows us to store multiple elements of similar data type under a single variable thereby saving us the effort of creating a different variable for every data. The arrays are helpful to create a list of elements of similar types, which can be accessed using their index or key. An array is created using an array() function in PHP. There are 3 types of array in PHP that are listed below:

  • Indexed Array: It is an array with a numeric key. It is basically an array wherein each of the keys is associated with its own specific value.
  • Associative Array: It is used to store key-value pairs.
  • Multidimensional Array: It is a type of array which stores another array at each index instead of a single element. In other words, define multi-dimensional arrays as array of arrays. 

For this purpose, we will use an associative array that uses a key-value type structure for storing data. These keys will be a string or an integer which will be used as an index to search the corresponding value in the array. The json_encode() function is used to convert the value of the array into JSON. This function is added in from PHP5. Also, you can make more nesting of arrays as per your requirement. You can also create an array of array of objects with this function. As in JSON, everything is stored as a key-value pair we will convert these key-value pairs of PHP arrays to JSON which can be used to send the response from the REST API server.

Example 1: The below example is to convert an array into JSON. 

PHP




<?php
  
  // Create an array that contains another
  // array with key value pair
  $arr = array (
  
      // Every array will be converted
      // to an object
      array(
          "name" => "Pankaj Singh",
          "age" => "20"
      ),
      array(
          "name" => "Arun Yadav",
          "age" => "21"
      ),
      array(
          "name" => "Apeksha Jaiswal",
          "age" => "20"
      )
  );
  
  // Function to convert array into JSON
  echo json_encode($arr);
?>
Output: 
[{"name":"Pankaj Singh","age":"20"},
{"name":"Arun Yadav","age":"21"},
{"name":"Apeksha Jaiswal","age":"20"}]

 

Example 2: This example illustrates the conversion of the 2D associative array into JSON.

PHP




<?php
  
  // Declare two dimensional associative
  // array and initialize it
  $arr = array (
      "first"=>array(
          "id"=>1,
          "product_name"=>"Doorbell",
          "cost"=>199
      ),
      "second"=>array(
          "id"=>2,
          "product_name"=>"Bottle",
          "cost"=>99
      ),
      "third"=>array(
          "id"=>3,
          "product_name"=>"Washing Machine",
          "cost"=>7999
      )
  );
  
  // Function to convert array into JSON
  echo json_encode($arr);
?>
Output: 
{"first":{"id":1,"product_name":"Doorbell","cost":199},
"second":{"id":2,"product_name":"Bottle","cost":99},
"third":{"id":3,"product_name":"Washing Machine","cost":7999}}

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!