How to create an array with key value pairs in PHP? Last Updated : 23 Sep, 2024 Comments Improve Suggest changes Like Article Like Report 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:Table of ContentBasic Key-Value Pair Creation and IterationShorthand Array Syntax with Additional FeaturesApproach 1: Basic Key-Value Pair Creation and IterationIn 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 FeaturesIn 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: Comment More infoAdvertise with us Next Article How to create an array with key value pairs in PHP? H Husban Follow Improve Article Tags : Technical Scripter Web Technologies PHP 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 Create an array filled with given values Using JavaScript In JavaScript, an array is a collection of values that can be stored and accessed in a single variable. Each value in an array is called an element, and elements can be of any data type, such as numbers, strings, or other objects. Arrays are indexed, meaning that each element has a numerical positio 5 min read Sort an Associative Array by Key in PHP Given an Associative Array, the task is to sort the associative array by its keys in PHP. There are different methods to sort Associative Array by keys, these are described below: Table of ContentUsing ksort() FunctionUsing uksort() FunctionConverting to a Regular Array for SortingUsing array_multis 3 min read How to convert an array to CSV file in PHP ? To convert an array into a CSV file we can use fputcsv() function. The fputcsv() function is used to format a line as CSV (comma separated values) file and writes it to an open file. The file which has to be read and the fields are sent as parameters to the fputcsv() function and it returns the leng 2 min read How to Slice an Array in PHP? In PHP, slicing an array means taking a subset of the array and extracting it according to designated indices. When you need to extract a subset of elements from an array without changing the original array, this operation comes in handy. PHP comes with a built-in function called array_slice() to he 2 min read How to Convert Number to Character Array in PHP ? Given a number, the task is to convert numbers to character arrays in PHP. It is a common operation when you need to manipulate or access individual digits of a number. This can be particularly useful in situations where you need to perform operations on the digits of a number, such as digital root 3 min read Like