Arrays in PHP: Part 206/21/2001
Today we'll continue our discussion of arrays in PHP including the use of associative and multidimensional arrays.
In my last article, we introduced a new array data type in PHP and covered its fundamentals. Today we'll dive deeper into arrays and introduce a type of array called an associative array and a more complex type of array -- the multidimensional array.
When we last discussed arrays, we described how arrays could be used and only deal with indexes based on integer numbers (1,2,3,etc). This week we'll introduce another type of array -- the associative arrays. Associative arrays are identical to any other type of array with one major difference -- their index values. In Associative arrays index values are not limited to simple integer values but can also be strings. For example, in an associative array the index "foo" can be used to store the value "bar". Thankfully, declaring and working with Associative arrays is identical to working with any other type of array. Here's an example:
<?php $myarray_1 = array("foo"=>"bar"); $myarray_2["foo"] = "bar"; ?>
As you can see, working with associative arrays and integer indexed arrays is nearly identical. But how can we use associative arrays to our advantage? With such flexibility, we are able to assign any string (such as a name) with any other string or data type (such as a phone number or e-mail address). Another example would be assigning meaningless error messages generated by your web site to a more useful description. These are just two examples of the usefulness of associative arrays. Next, we'll discuss ways to embed arrays within other arrays to create what is called a multidimensional array.
As we just stated, a multidimensional array is an array that contains at least one other array as the value of one of the indexes. An example of a multidimensional array would be an array
$foo that has values for the indexes 1 through 5 with index value 2 containing another array. Creating and working with multidimensional arrays can be done a number of ways -- all of which we introduced in part 1 of this series. For example, in part 1 we discussed how to create a single-dimensional array using the array statement as follows:
<?php $foo = array(0=>"abc", 1=>"def", 2=>"ghi"); ?>
Where the three indexes 0,1, and 2 are associated with different letters in the alphabet. In a multidimensional array, we can simply replace one of those associations with one that points to an array rather than a string constant. For example:
<?php $emails = array("john"=>"email@example.com", "dan"=>"firstname.lastname@example.org", "rachel"=>"email@example.com"); $foo = array(0=>"abc", 1=>$emails, 2=>"ghi"); ?>
As you can see, we have replaced index 1 in our
$foo array with the variable
$emails (also an array) and therefore creating a multidimensional array. Also note that we could have simply appended
$foo automatically using the syntax we introduced in part 1 of the series:
<?php $foo = $emails; ?>
Using the above would have created a new integer index 3 and associated with it the array
$emails. Once we have created this association, we can add a whole new index to our
$emails array that is embedded within
$foo by the following syntax:
<?php $foo['steve'] = "firstname.lastname@example.org";
Also in PHP Foundations:
Because index 3 of
$foo is an array, we can reference that array by appending an additional set of brackets to the
$foo variable. In the above example, the result would be a fourth entry with the index value of "steve" and data value of "email@example.com" being created in the array located at the 3rd index of the array
$foo. It's a little confusing, but the best way to look at it is to remember that
$foo is really no different than
$foo_3 (a normal variable name) in practice. When you reference an index of an array directly, the rest of the contents of the array are inconsequential and is best to disregard when trying to visualize complex multidimensional arrays.
More on working with and getting data from arrays
To wrap up our two-part series on arrays, we'll briefly touch on some functions that will help you work with arrays (they will be discussed further in future issues), and introduce a new control structure that will greatly enhance the power of arrays in PHP. We'll start with some useful functions available to PHP developers to help them gather information and then introduce our new control structure (
As we already mentioned, PHP has a wealth of pre-programmed functions for manipulating, analyzing, and processing arrays built into the language. For a complete list of these functions, please visit the PHP web manual reference for arrays. Almost all of them are very useful under the right circumstances so we'll only discuss the ones here that are necessary for just about any application of arrays in PHP. These functions are:
Pages: 1, 2