JSON Array [PHP Code]

Arrays in JSON are used to store a list of primitive or complex objects. JSON arrays can store different types of elements: strings, numbers, booleans, objects, and multidimensional arrays. The values of a JSON array are separated by commas. Array elements can be accessed by using the "[]" operator. Unlike dictionaries, where you can get the value by its key, in a JSON array, the array elements can only be accessed by their index. The following is an example of a JSON array with numbers. Below, you can find a list of JSON arrays with different data types. The PHP code was automatically generated for the JSON Array example.
JSON Array [PHP Code] Format
[0, 1, 1, 2, 3, 5, 8, 13, 21]
Updated: Viewed: 29582 times
PHP code for JSON Array example

PHP code for JSON Array Example

This PHP code snippet was generated automatically for the JSON Array example.
<< Back to the JSON Array example

What is JSON?

JavaScript Object Notation (JSON) is a lightweight, language-independent, text-based data exchange format. JSON specifies a set of rules for representing structured data in a portable manner. JSON supports four primitive types (strings, numbers, boolean values, and null) and two structured types (objects and arrays). Many programming languages use JSON to exchange data, including JavaScript, Java, C++, C#, Go, PHP, Python, and many others.

JSON Example
{
  "Id": 78912,
  "Customer": "Jason Sweet",
  "Quantity": 1,
  "Price": 18.00
}

JSON Array Examples

JSON arrays come in different types: strings, numbers, booleans, objects, and multidimensional arrays. Below are examples of JSON array types with a detailed description:

JSON Array of Strings

The JSON array of strings contains only string elements, where each element is separated by a comma (,). The following is an example of a JSON array storing string values:

JSON Strings Array Example
["Apple", "Cherry", "Strawberry", "Banana", "Grape"]

JSON Array of Numbers

The JSON array of numbers contains only numeric elements. Below is an example of a JSON array storing numeric values:

JSON Numbers Array Example
[13, 25, 53, 78, 94]

JSON Array of Booleans

The JSON array of booleans contains only booleans (either true or false). The following is an example of an array of JSON booleans:

JSON Booleans Array Example
[true, true, false, false, true]

JSON Array of Objects

A JSON object is similar to a JavaScript object. We can also create a JSON array containing many objects, and then we can iterate over this array or use "[]" square brackets to get the desired object. Below is an example of an array of JSON objects:

JSON Objects Array Example
[
  {"name":"Leo", "rating":"[email protected]", "age":25},
  {"name":"Tim", "rating":"[email protected]", "age":23},
  {"name":"Alice", "rating":"[email protected]", "age":31},
  {"name":"Jack", "rating":"[email protected]", "age":27}
]

JSON Nested Array

JSON arrays can contain various arrays as elements. You can use the "[]" operator to get an array at any index and use the "[]" operator again to get an element of the selected array. The following is an example of creating a multidimensional JSON array:

JSON Nested Array Example
[    
  ["a", "a", "a"],   
  ["b", "b", "b"],   
  ["c", "c", "c"]   
]

JSON Mixed Array

The mixed JSON array contains various types of elements. Below is an example of a JSON array that stores mixed values:

JSON Mixed Array Example
["Apple", 1, true,  {"name":"Leo", "rating":"VIP"}]

See also

Generate code snippets for PHP and other programming languages

Convert your JSON Array request to the PHP, JavaScript/AJAX, Node.js, Curl/Bash, Python, Java, C#/.NET code snippets using the PHP code generator.