In this tutorial, we will be covering PHP associative arrays and how you can create, edit, and access them.
In PHP, associative arrays consist of elements that feature a key-value pair. To explain further, each key in an array will have a value attached to it. To access a specific value, you need to reference the key associated with that value.
When deciding to use an associative array instead of an index array or object comes down to personal preference and the data you wish to store. An associative array works best when you have data you can tie to a term. For example, you can have a key named age with the person’s age stored as a value.
The topics below will take you through the basics of using associative arrays in your PHP program. They are a relatively easy concept to understand, so I hope the information below helps.
Creating an Associative Array
Creating an associative array is relatively simple. There are two different methods you can use to create the array. The first method involves creating all the key value pairs in a single array block. The second method involves creating a key and assigning it a value one by one.
The example below creates the associative array within a single array block. We use the shortened array syntax, but you can use the array()
function instead.
<?php
$grades = ["Jerry" => 54,
"Sally" => 86,
"Jordan" => 84,
"Emily" => 94,
];
print_r($grades);
In our second example, we build our associative array by defining our key value pair one at a time.
<?php
$grades["Jerry"] = 54;
$grades["Sally"] = 86;
$grades["Jordan"] = 84;
$grades["Emily"] = 94;
print_r($grades);
If we print either of the above examples into the terminal or web browser, we will get the following output.
Array ( [Jerry] => 54 [Sally] => 86 [Jordan] => 84 [Emily] => 94 )
Editing an Associative Array
In this section, we will touch on the different ways you can alter an associative array. For example, we will cover adding a new element, editing an existing element, or removing an element. All these examples should give you a basic understanding of handling these arrays.
Add a New Element
To add a new element to an associative array, you can specify a new key and assign a value to it. Other methods involve using array_push()
to add the new element to the end of the array.
The example below demonstrates how you can add a new element to an existing associative array.
<?php
$grades = ["Jerry" => 54,
"Sally" => 86,
"Jordan" => 84,
"Emily" => 94,
];
$grades["Bob"] = 42;
Using echo and print_r()
, we output the array before and after adding the new element.
Before
Array ( [Jerry] => 54 [Sally] => 86 [Jordan] => 84 [Emily] => 94 )
After
Array ( [Jerry] => 54 [Sally] => 86 [Jordan] => 84 [Emily] => 94 [Bob] => 42 )
Edit an Existing Element
To edit an existing element in an associative array, reference the key and assign a new value to it. In the example below, we update the array element with the key “Jerry” to contain 92 instead of 54.
<?php
$grades = ["Jerry" => 54,
"Sally" => 86,
"Jordan" => 84,
"Emily" => 94,
];
$grades["Jerry"] = 92;
In the output below, you can see that Jerry now contains our new value.
Array ( [Jerry] => 92 [Sally] => 86 [Jordan] => 84 [Emily] => 94 )
Remove an Element
You can use unset to remove an entire element from an associative array. Specify the array and key inside the unset function, and it will remove the element.
The example below will remove the element with the key “Jerry“.
<?php
$grades = ["Jerry" => 54,
"Sally" => 86,
"Jordan" => 84,
"Emily" => 94,
];
unset($grades["Jerry"]);
print_r($grades);
Our output below shows that the element with the key “Jerry” has been removed from our array.
Array ( [Sally] => 86 [Jordan] => 84 [Emily] => 94 )
Accessing Data Stored in an Associative Array
This section will go through the two most popular methods for accessing data stored within an associative array. Of course, there are other methods that you can use, but this tutorial will focus on the basics.
Accessing a Value Directly
To access a value directly in an associative array, reference the key associated with the value. In the example below, we print the value of the element that has “Jordon” as a key.
<?php
$grades = ["Jerry" => 54,
"Sally" => 86,
"Jordan" => 84,
"Emily" => 94,
];
echo $grades["Jordan"];
As you would expect, the value printed to the browser or terminal will be 84.
84
Looping through the Array
You can easily loop through an associative array and print each of the keys and the associated values.
Below is an example of how you can print each key and value using a foreach loop and echo.
<?php
$grades = ["Jerry" => 54,
"Sally" => 86,
"Jordan" => 84,
"Emily" => 94,
];
foreach($grades as $key => $value) {
echo $key . " => " . $value;
echo "<br>";
}
Below is the output of the above script. As you can see, each key and the associated value are printed to the browser or terminal.
Jerry => 54
Sally => 86
Jordan => 84
Emily => 94
You can use the foreach loop for updating values, removing values, and much more. It is a great way to traverse an associative array.
Conclusion
I hope that you now have a decent understanding of associative arrays in PHP. We touched on creating, editing, and accessing the arrays. If you are using PHP, you will likely need to use or understand this type of array.
We have plenty of other PHP tutorials that I highly recommend that you take a look at if you have the time. For example, our guide to arrays will help you understand the other types of arrays you can use in PHP.
If we can improve this tutorial, please do not hesitate to let us know by leaving a comment below.