How to create an array with key value pairs in PHP?
Last Updated :
23 Sep, 2024
In PHP, an array with key-value pairs is called an associative array. It maps specific keys to values, allowing you to access elements using custom keys rather than numerical indices. Keys are strings or integers, while values can be of any data type.
Here we have some common approaches to create an array with key value pairs in PHP:
Approach 1: Basic Key-Value Pair Creation and Iteration
In this approach, we create an associative array where the keys represent social media platforms, and the values are descriptions of these platforms. After adding a new element, we loop through the array and display each key-value pair in an HTML paragraph.
Example 1: In this example we creates an associative array $websites with social media platforms as keys and their descriptions as values. It then adds "Instagram" and loops through the array, printing each key-value pair in paragraph tags.
php
<?php
$websites = array("Facebook" =>
"Facebook, Inc. is an online social media and
social networking service company.",
"Twitter" =>
"Twitter is a microblogging and social networking service on
which users post and interact with messages known as tweets.",
"LinkedIn" =>
"LinkedIn is a business and employment-oriented service
that operates via websites and mobile apps.");
$websites["Instagram"] = "Instagram is a photo and video-sharing
social networking service owned by Facebook, Inc.";
foreach ($websites as $key => $value){
echo "<p>$key: $value <p>";
}
?>
Output:

Approach 2: Shorthand Array Syntax with Additional Features
In this approach, we utilize the shorthand notation for arrays and add extra key-value pairs dynamically. This method also demonstrates how different data types can be used as values.
Example 2: In this example we defines an associative array of social media platforms and their descriptions, adds Instagram, and loops through the array, printing each key-value pair in paragraphs.
php
<?php
$websites = ["Facebook" =>
"Facebook, Inc. is an online social
media and social networking service company.",
"Twitter" => "Twitter is a microblogging and social networking service
on which users post and interact with messages known as tweets.",
"LinkedIn" => "LinkedIn is a business and
employment-oriented service that operates via websites and mobile apps."];
$websites["Instagram"] = "Instagram is a photo and video-sharing
social networking service owned by Facebook, Inc.";
foreach ($websites as $key => $value){
echo "<p>$key: $value <p>";
}
?>
Output:
Similar Reads
How to Create an Object Without Class in PHP ? In PHP, creating an object without a class refers to creating an instance of the stdClass, a built-in PHP class for creating generic objects. It allows developers to instantiate objects without explicitly defining a custom class, often used for dynamic data storage.In this article, we will create an
3 min read
How to get specific key value from array in PHP ? In this article, we will see how to get specific key values from the given array. PHP array is a collection of items that are stored under keys. There are two possible types of keys: strings and integers. For any type of key, there is a common syntax to get a specific value by key â square brackets.
3 min read
How to Populate Dropdown List with Array Values in PHP? We will create an array, and then populate the array elements to the dropdown list in PHP. It is a common task when you want to provide users with a selection of options. There are three approaches to achieve this, including using a foreach loop, array_map() function, and implode() function. Here, w
2 min read
How to use array_merge() and array_combine() in PHP ? In this article, we will discuss about how to use array_merge() and array_combine() functions in PHP. Both functions are array based functions used to combine two or more arrays using PHP. We will see each function with syntax and implementation array_merge() FunctionThis function merges the two or
3 min read
How to check foreach Loop Key Value in PHP ? In PHP, the foreach loop can be used to loop over an array of elements. It can be used in many ways such asTable of ContentUsing the for-each loop with simple valuesUsing the foreach loop with Key-Value pairsUsing array_keys Function to Access Keys and ValuesUsing array_walk Function for IterationUs
3 min read