Associative Arrays in PHP

Learn via video courses
Topics Covered

Overview

Associative arrays are an essential data structure in PHP that allows developers to store and manipulate collections of values using named keys instead of numeric indexes. They are also known as maps or dictionaries in other programming languages. An associative array is created using the array() function with a set of key-value pairs, where each key represents a unique identifier for a value in the array. These keys can be of any data type, including strings, integers, and floats.

Introduction

An associative array is a type of array in PHP that uses named keys instead of numeric keys to access and store values. Unlike indexed arrays, where values are stored and accessed using sequential integer keys, associative arrays use string keys that are associated with specific values.

This means that developers can easily access values in an associative array by referring to their associated keys, rather than having to remember or calculate the index numbers of each value.

Two Ways to Create an Associative Array

An associative array in PHP is a type of array that uses string keys instead of numeric indices. Each key in the array is associated with a specific value.

Here are two ways to create an associative array in PHP:

  1. Using the array() Function:
    You can create an associative array in PHP using the array() function. This function takes a list of key-value pairs separated by commas. Each key-value pair is written as a key => value, where the key is the string key and the value is the value associated with the key.

Here's an Example:

  1. Using the Square Bracket Notation:
    Another way to create an associative array in PHP is by using the square bracket notation. This involves defining the array and then assigning key-value pairs to it using the square bracket notation.

Here's an example:

In this example, we've defined an empty array called $student and then assigned three key-value pairs to it using the square bracket notation. Each key-value pair is assigned using the syntax $array[key] = value. In this case, we're assigning the values Golf Green, 25, and Computer Science to the keys name, age, and major, respectively.

Loop Through an Associative Array

In PHP, you can use a loop to iterate through an associative array and perform actions on each key-value pair. Two main types of loops can be used to iterate through an associative array: the foreach loop and the while loop.

Here's an example of how to loop through an associative array using a foreach loop:

Output:

In this example, we're defining an associative array called $student with three key-value pairs. We're then using a foreach loop to iterate through each key-value pair in the array. Inside the loop, we're using the $key and $value variables to access the key and value of each pair, respectively. We're then using the echo statement to output the key and value on separate lines.

You can also use a while loop to iterate through an associative array.

Here's an example of how to do that:

Output:

Traversing the Associative Array

Traversing an associative array in PHP involves iterating through each key-value pair in the array and performing some operation on it.

Here are several ways to traverse an associative array in PHP:

Using a foreach Loop:

The simplest way to traverse an associative array in PHP is by using a foreach loop. The foreach loop iterates through each key-value pair in the array and assigns the key to a variable (in this case, $key) and the value to another variable (in this case, $value).

Here's an Example:

Using a While Loop:

Another way to traverse an associative array in PHP is by using a while loop and the each() function. The each() function returns the current key-value pair in the array as an array with two elements: the key and the value.

Here's an Example:

In this example, we're iterating through the $student array using a while loop and the each() function. The while loop continues as long as each() returns a non-empty array, which contains the current key-value pair. Inside the loop, we extract the key and value from the array using the 'key' and 'value' indices, respectively. The echo statement inside the loop prints out the key and value for each iteration.

Using a For Loop:

Finally, you can also traverse an associative array in PHP using a for loop and the array_keys() function. The array_keys() function returns an array of the keys in the associative array, which can then be used to access the corresponding values using the square bracket notation.

Here's an example:

In this example, we're first getting an array of the keys in the $student array using the array_keys() function. We then iterate through the keys using a for loop and access the corresponding values using the square bracket notation. The echo statement inside the loop prints out the key and value for each iteration.

Creating an Associative Array of Mixed Types

Creating an associative array of mixed types in PHP is similar to creating a regular associative array, except that you can store values of different data types in the same array.

Here's an example of how to create an associative array of mixed types in PHP:

To access the values in this associative array, you can use the square bracket notation or the curly brace notation. For example, to access the value of the "name" key, you can use the following code:

Output:

To access the value of the "hobbies" key, which is an array, you can use the following code:

Output:

You can also loop through the associative array using a foreach loop. , For example,, to loop through all the key-value pairs in the $person array, you can use the following code:

Output:

In this example, we've used the foreach loop to iterate through each key-value pair in the $person array. The $key variable contains the string key, and the $value variable contains the corresponding value. Note that the boolean value "true" is printed as "1" in the output.

Conclusion

  • An associative array in PHP is a type of array that uses string keys instead of numeric indices.
  • Associative arrays are useful when we need to store key-value pairs and access them using string keys.
  • We can create an associative array in PHP using the array() function or the square bracket notation.
  • Associative arrays can store values of different data types, including strings, integers, floats, booleans, and arrays.

MCQs

  1. Which of the following is true about associative arrays in PHP?
    a) They can only store values of the same data type
    b) They are indexed by numeric values
    c) They use string keys to associate values
    d) They can be accessed using only the square bracket notation.
    Answer: c)

  2. How do you add a new key-value pair to an existing associative array in PHP?
    a) Using the array_push() function
    b) Using the push() method
    c) Using the array_merge() function
    d) Using the square bracket notation
    Answer: d)

  3. Which of the following is a valid way to loop through an associative array in PHP?
    a) for loop
    b) while loop
    c) foreach loop
    d) do-while loop
    Answer: c)