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 dictionary and add key–value pairs dynamically?
- 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 unequal element pairs from the given Array
- Count pairs in an array which have at least one digit common
- Check if all elements of the given array can be made 0 by decrementing value in pairs
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.