How to create an array for JSON using PHP? - GeeksforGeeks

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);?>
[{"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);?>
{"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}}
 
sweetyty
bhaskargeeksforgeeks
JSON
PHP-Questions
Picked
PHP
PHP Programs
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to fetch data from localserver database and display on HTML table using PHP ?
How to pass form variables from one page to other page in PHP ?
Create a drop-down list that options fetched from a MySQL database in PHP
How to create admin login page using PHP?
Different ways for passing data to view in Laravel
How to call PHP function on the click of a Button ?
How to fetch data from localserver database and display on HTML table using PHP ?
How to pass form variables from one page to other page in PHP ?
How to create admin login page using PHP?
How to Install php-curl in Ubuntu ?