![]() |
Indexed and Associative Arrays |
Indexed ArraysArrays are a very important variable type. In php there is more than one way represent arrays. There is the index array, which we have just seen and the associative array which we will be taking a look at here. An array is an "indexed" set of values that are referenced by these indexes. The difference between the two types of array structures is how the two are indexed. Integer indexed arrays use integers as indexes. The first array index is 0, and proceeds upwards to the last value in the array. Performing operations on these type of arrays is very easy. You can use a construct like a for loop to cleanly operate on the entire array. Like so: /* where $i is a counter variable Assiociative ArraysAssociate arrays are not indexed by integers, they are indexed by a string. This creates so interesting problems for searching, sorting and operating on these arrays. So why would you want to use such a construct? Imagine if wanted to consolidate some information about a user and you wanted to use an array. You could do something like:$user1[0] = "Keith Geffert"; /* Users Full Name */But now you must remember (in all parts of your code) what index value represents what user information. This makes things very difficult in the long run. So a year from now and 3 websites later, your using index 0 for the email address, 3 for the full name and 2 for the title you decide to add a few things and add incorrect code because you accidentally forgot your using different indexes now. Could there be a better way? $user1['full_name'] = 'Keith Geffert';Whoa! Does't that make a big difference? Now it's really easy to understand what those indexes are about. And since we consider the $user1 array as a group of tid bits about a user we do not care what order or index position each value has, as long as, the associative values stay equivalent. Operating on Associative ArraysSince we can not use integer indexes to iterate over an associative array how to we perform operations on these animals? PHP provides special built-in functions to aid you in your struggle to tame these beasts. What if you wanted to print out on a web page some user information from the array above but want to make sure that all the html special characters are converted to the equivalent html codes such as the less that and greater than sign? (email addresses often take the form of '<Keith Geffert> kgeffert@mlinux.org') HTML does not allow us to just print out <, or > we must translate them to < and > respectively. The built-in php function htmlspecialchars() does just this. Ex:/* replace all >'s and <'s with HTML &xxx; codes */ Arrays of ArraysSince each array value can be any valid type, you can have arrays of arrays. These are called multi-dimensional arrays. A 2 dimensional array could be looked at as a 2 dimensional plane where one corner has the location (0,0). At each point in the plane a value could be stored. Referencing these types of variables is just like all other arrays. To assign a value to coordinate 4,3 you could do this: $d2_array[ 4 ][ 3 ] = 5; Maybe this represents the height of some surface at this point. What about 3 dimensions? $d3_array [ 4 ][ 3 ][ 5 ] = 32; This could possibly be the temperature at the point (4,3,5) in some spacial cube.So what does that have to do with web programming? An easy example might be. You have compiled a user list using the associative array above. But are using an array to hold all of the user arrays. To further complicate matters, you have indexed this array using the members username. $users['someuser'] <--- this references the member array for user "someuser"Now lets say you wanted to make sure you translated all HTML special characters for all the users in the $users array. You need to use a nested looping structure like so: /* replace all >'s and <'s with HTML &xxx; codes */Maybe there could be a different approach? Some variable type that can incorporate all this together so we don't have to constantly write all these loops all the time and complicated ones at that? |