Tutorial 5 : Arrays What Is an Array? You already know that a variable is a "bucket" in which you can temporarily store a value. By using variables, you can create a script that stores, processes, and outputs different information every time it is run. Unfortunately, you can only store one value at a time in a variable. Arrays are special variables that allow you to overcome this limitation. An array allows you to store as many values as you want in the same variable. Each value is indexed within the array by a number or a string. If a variable is a bucket, you can think of an array as a filing cabinet— a single container that can store many discrete items. Creating Arrays By default, arrays are lists of values indexed by number. Values can be assigned to an array in two ways: with the array() function or directly using the array identifier []. You'll meet both of these in the next two sections. Defining Arrays with the array() Function The array() function is useful when you want to assign multiple values to an array at one time. Let's define an array called $users and assign four strings to it: $users = array ("Bert", "Sharon", "Betty", "Harry" ); You can now access the third element in the $user array by using the index "2": print "$users[2]"; This would return the string " Defining or Adding to Arrays with the Array Identifier You can create a new array (or add to an existing one) by using the array identifier in conjunction with the array name. The array identifier is a set of square brackets with no index number or name inside it. Let's re-create our $users array in this way: $users[ ] = " Bert"; $users[ ] = " $users[ ] = " Betty"; $users[ ] = " Harry"; Notice that we didn't need to place any numbers between the square brackets. PHP4 automatically takes care of the index number, which saves you from having to work out which is the next available slot. We could have added numbers if we wanted, and the result would have been exactly the same. It's not advisable to do this, though. Take a look at the following code: $users[0] = " Bert"; $users[200] = " The array has only two elements, but the index of the final element is 200. PHP4 will not initialize the intervening elements. This could lead to confusion when attempting to access elements in the array. In addition to creating arrays, you can use the array identifier to add new values onto the end of an existing array. In the following code, we define an array with the array() function and use the array identifier to add a new element: $users = array ("Bert", " Sharon", "Betty", "Harry" ); $users[] = "sally"; Defining Associative Arrays with the array() Function To define an associative array with the array() function, you must define both the key and value for each element. The following code creates an associative array called $character with four elements: $character = array ( name=>"bob", occupation=>"superhero", age=>30, "special power"=>"x-ray vision" ); We can now access any of the fields of $character: print $character[age]; The keys in an associative array are strings, but it isn't necessary to surround them with quotation marks unless the key contains more than one word. Directly Defining or Adding to an Associative Array You can create or add a name/value pair to an associative array simply by assigning a value to a named element. In the following, we re-create our $character array by directly assigning a value to each key: $character[name] = "bob"; $character[occupation] = "superhero"; $character[age] = 30; $character["special power"] = "x-ray vision"; Multidimensional Arrays Until now, we've simply said that elements of arrays are values. In our $character array, three of the elements held strings, and one held an integer. The reality is a little more complex, however. In fact, an element of an array could be a value, an object, or even another array. A multidimensional array is an array of arrays. Imagine an array that stores an array in each of its elements. To access the third element of the second element, you would have to use two indices: $array[1][2] <?php $characters = array ( array ( name=>"bob", occupation=>"superhero", age=>30, specialty=>"x-ray vision" ), array ( name=>"sally", occupation=>"superhero", age=>24, specialty=>"superhuman strength" ), array ( name=>"mary", occupation=>"arch villain", age=>63, specialty=>"nanotechnology" ) ); print $characters[0][occupation]; ?> Notice that we have nested array function calls within an array function call. At the first level, we define an array. For each of its elements, we define an associative array. Accessing $user[2], therefore, gives us access to the third associative array in the top-level array. We can then go ahead and access any of the associative array's fields. $user[2][name] will be "mary", and $user[2][age] will be 63. When this concept is clear, it will be easy to create complex combinations of associative and numerically indexed arrays. |