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 ?
- Count of even and odd power pairs in an Array
- 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
- How to create To-Do List using jQuery?
- Create a Search Bar using HTML and CSS
- How to create a Collapsed Sidebar?
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.