How to create an array for JSON using PHP? Last Updated : 03 Dec, 2021 Comments Improve Suggest changes Like Article Like Report 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}} Comment More infoAdvertise with us Next Article How to create an array for JSON using PHP? P Pankaj_Singh Follow Improve Article Tags : Web Technologies PHP PHP Programs JSON PHP-Questions +1 More Similar Reads How to create a string by joining the array elements using PHP ? We have given an array containing some array elements and the task is to join all the array elements to make a string. In order to do this task, we have the following methods in PHP:Table of ContentUsing implode() Method: Using join() MethodUsing array_reduce()Using a Foreach LoopUsing array_map() w 3 min read How to check whether an array is empty using PHP? In PHP, arrays are commonly used data structures that can hold multiple values. However, sometimes it is necessary to check whether an array is empty, i.e., whether it has no elements. This is useful when the array is filled with data automatically, and you need to check if it has any data before pe 3 min read How to Convert Byte Array to JSON in PHP ? Given a Byte Array, the task is to convert Byte Array into JSON using PHP. Converting a byte array to JSON in PHP is a common task, especially when dealing with binary data or when you want to represent raw data in a JSON format. Table of Content Using base64_encode() and json_encode() FunctionsUsin 2 min read How to Create an Array of Given Size in PHP? This article will show you how to create an array of given size in PHP. PHP arrays are versatile data structures that can hold multiple values. Sometimes, it's necessary to create an array of a specific size, either filled with default values or left empty. Table of Content Using array_fill() Functi 3 min read How to Create HTML List from Array in PHP? Given an array containing some items, the task is to create an HTML list from an array in PHP. An HTML list is a collection of items enclosed within <ul> (unordered list) or <ol> (ordered list) tags. Each item in the list is enclosed within <li> (list item) tags. This article explo 3 min read How to Get All Values from an Associative Array in PHP? Given an Associative Array, the task is to get all values from the associative array in PHP. To get all the values from an associative array, you can use various approaches such as foreach loop, array_values() function, and array_map() function. In this article, we will explore each approach with de 3 min read How to append data in JSON file through HTML form using PHP ? The purpose of this article is to append data to a JSON file through HTML form using PHP. Approach 1: If the JSON file is not created then we create a new JSON file, send data to it, and append data in it. To see how to create a JSON file by taking data from the HTML form, refer this link. Approach 4 min read How to Create Comma Separated List from an Array in PHP? The comma-separated list can be created by using implode() function. The implode() is a builtin function in PHP and is used to join the elements of an array.Syntax:string implode( separator, array )Return Type:The return type of implode() function is string. It will return the joined string formed f 2 min read Convert a String into an Array of Characters in PHP Given a string, the task is to convert the string into an array of characters using PHP. Examples:Input: str = "GFG" Output: Array( [0] => G [1] => f [2] => G)Input: str = "Hello Geeks"Output: Array( [0] => H [1] => e [2] => l [3] => l [4] => o [5] => [6] => G [7] => 4 min read How to Convert Query String to an Array in PHP? In this article, we will see how to convert a query string to an array in PHP. The Query String is the part of the URL that starts after the question mark(?).Examples:Input: str = "company=GeeksforGeeks&address=Noida&mobile=9876543210"Output: Array ( [company] => GeeksforGeeks [ad 4 min read Like