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 dictionary and add key–value pairs dynamically?
- Difference between Primary key and Unique key
- Difference between Primary key and Super key
- How to search by key=>value in a multidimensional array in PHP ?
- How to search by multiple key => value in PHP array ?
- How to remove a key and its value from an associative array in PHP ?
- PHP | Program to move (key,value) pair upwards or downwards
- How to delete an array element based on key in PHP?
- How to loop through an associative array and get the key in PHP?
- How to check a key exists in an array in PHP ?
- HashMap replace(key, value) method in Java with Examples
- HashMap putIfAbsent(key, value) method in Java with Examples
- HashMap merge(key, value, BiFunction) method in Java with Examples
- How to find record using any key-value pair information of record in your local/custom database using Node.js ?
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.