Arrays are wonderful constructs in programming. Languages universally have arrays which use an integer based index. (0-N, where N is the number of elements – 1) This creates an “order” to the array which can be iterated without needing to know about the content of the array.

Associative arrays are alittle different. An associative array uses strings as identifiers so that elements can be accessed by an understood key or hash. This is relevant when considering how database information is stored and returned. Fields in a table use names, and these names are given meanings. This helps us to work with this data in a more relational manner, instead of an iterative approach.

In PHP this can been understood as $my_users[“first_name”]; Let’s assume this references an array of data returned from a database query earlier in the script, which accesses our user table and returns all fields, including a field named “first_name”. We would then expect this reference to return our user’s first name. This is much more adaptable then using integers to refer to fields if we consider that the columns of the table may change as the application matures. For example, a security question, or phone number may be added to the table, meaning any positional references to fields in the table would need to be shifted. I’m sure you can see how this could become problematic especially in a multiple developer environment.

In C# .Net this association can be accomplished but in a slightly different way. C# has a useful class called a Dictionary. This is defined by stating:

Dictionary my_data = new Dictionary();

This will create a variable called “my_data” which has indexes of type string, and values of type string. We can then reference the values in this using our syntax from the PHP section, for example: my_data[“first_name”];