PHP Associative Arrays

In this tutorial, we will be covering PHP associative arrays and how you can create, edit, and access them.

PHP Associative Arrays

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.


In our second example, we build our associative array by defining our key value pair one at a time.


If we print either of the above examples into the terminal or web browser, we will get the following output.


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.


Using echo and print_r(), we output the array before and after adding the new element.


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.


In the output below, you can see that Jerry now contains our new value.


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“.


Our output below shows that the element with the key “Jerry” has been removed from our array.


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.


As you would expect, the value printed to the browser or terminal will be 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.


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.


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.

Print Friendly, PDF & Email

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Server Status

Aradippou Chat 1 Chat 2 Larnaca Limassol Nicosia

Chat Links

Official Links.

1. Chat 1.
2. Chat 2.

Alternative Mirror Links.

1. KiwiIRC 1.
2. KiwiIRC 2.

Other Web Clients.

1. IrcCloud.

Recent Posts

Related Posts:

Advertisement

Archives

Super Club Radio

Mighty Deals

CyIRC

CyIRC Tweets

Scroll Up
Fatal error: Uncaught WMAC\JSMin_UnterminatedStringException: WMAC\JSMin: Unterminated String at byte 1181: "+o.accessToken}}).then(e=>{if(!e.ok)throw e;return e});return a.catch(e=>{console.error(`Failed to logout chatrix session. deviceId: ${o.deviceId}`)}),a}})(); in /home/bszucbfe/public_html/wp-content/plugins/clearfy/components/minify-and-combine/includes/classes/ext/php/jsmin.php:215 Stack trace: #0 /home/bszucbfe/public_html/wp-content/plugins/clearfy/components/minify-and-combine/includes/classes/ext/php/jsmin.php(157): WMAC\JSMin->action(2) #1 /home/bszucbfe/public_html/wp-content/plugins/clearfy/components/minify-and-combine/includes/classes/ext/php/jsmin.php(96): WMAC\JSMin->min() #2 /home/bszucbfe/public_html/wp-content/plugins/clearfy/components/minify-and-combine/includes/classes/class-main.php(375): WMAC\JSMin::minify('(function(){"us...') #3 /home/bszucbfe/public_html/wp-includes/class-wp-hook.php(324): WMAC_PluginMain->jsSnippetcacher('(function(){"us...', '/home/bszucbfe/...') #4 /home/bszucbfe/public_h in /home/bszucbfe/public_html/wp-content/plugins/clearfy/components/minify-and-combine/includes/classes/ext/php/jsmin.php on line 215