PHP offers us a special type of array called an Associative Array that allows us to create an array with Key-Value pairs. The syntax for creating an Associative Array is as follows:
Syntax 1: Using array() constructor
$arrayVariable = array( key1 => value1, key2 => value2, key3 => value3, ... keyN => valueN, );
Syntax 2: Using shorthand notation
$arrayVariable = [ key1 => value1, key2 => value2, key3 => value3, ... keyN => valueN, ];
- The comma after the last Key-Value pair is optional.
- The Key can be of either integer or string type.
- The Value can be of any valid type.
- If the type of Key is other that string or integer, it will be cast to string or integer depending on the type of Key.
Example 1: Using array() constructor
Example 2: Using shorthand notation
- How to create an unordered_map of pairs in C++?
- How to create an array for JSON using PHP?
- Create a Pandas Series from array
- How to create comma separated list from an array in PHP ?
- Print all pairs in an unsorted array with equal sum
- Count pairs in an array which have at least one digit common
- Ways to form n/2 pairs such that difference of pairs is minimum
- PHP | Collator create() Function
- jQuery | Create a div element
- How to create a div element in jQuery ?
- Different ways to create an Object in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.