Home > Articles > Programming > PHP

  • Print
  • + Share This
This chapter is from the book

Arrays with Different Indices

In the $products array, you allowed PHP to give each item the default index. This meant that the first item you added became item 0; the second, item 1; and so on. PHP also supports arrays in which you can associate any key or index you want with each value.

Initializing an Array

The following code creates an array with product names as keys and prices as values:

$prices = array( 'Tires'=>100, 'Oil'=>10, 'Spark Plugs'=>4 );

The symbol between the keys and values is simply an equal sign immediately followed by a greater than symbol.

Accessing the Array Elements

Again, you access the contents using the variable name and a key, so you can access the information stored in the prices array as $prices[ 'Tires' ], $prices[ 'Oil' ], and $prices[ 'Spark Plugs' ].

The following code creates the same $prices array. Instead of creating an array with three elements, this version creates an array with only one element and then adds two more:

$prices = array( 'Tires'=>100 );
$prices['Oil'] = 10;
$prices['Spark Plugs'] = 4;

Here is another slightly different but equivalent piece of code. In this version, you do not explicitly create an array at all. The array is created for you when you add the first element to it:

$prices['Tires'] = 100;
$prices['Oil'] = 10;
$prices['Spark Plugs'] = 4;

Using Loops

Because the indices in an array are not numbers, you cannot use a simple counter in a for loop to work with the array. However, you can use the foreach loop or the list() and each() constructs.

The foreach loop has a slightly different structure when using associative arrays. You can use it exactly as you did in the previous example, or you can incorporate the keys as well:

foreach ($prices as $key => $value)
 echo $key.'=>'.$value.'<br />';

The following code lists the contents of the $prices array using the each() construct:

while( $element = each( $prices ) )
{
 echo $element[ 'key' ];
 echo ' - ';
 echo $element[ 'value' ];
 echo '<br />';
}

The output of this script fragment is shown in Figure 3.2.

Figure 3.2Figure 3.2 An each() statement can be used to loop through arrays.

In Chapter 1, you looked at while loops and the echo statement. The preceding code uses the each() function, which you have not used before. This function returns the current element in an array and makes the next element the current one. Because you are calling each() within a while loop, it returns every element in the array in turn and stops when the end of the array is reached.

In this code, the variable $element is an array. When you call each(), it gives you an array with four values and the four indices to the array locations. The locations key and 0 contain the key of the current element, and the locations value and 1 contain the value of the current element. Although the one you choose makes no difference, we chose to use the named locations rather than the numbered ones.

There is a more elegant and more common way of doing the same thing. The construct list() can be used to split an array into a number of values. You can separate two of the values that the each() function gives you like this:

list( $product, $price ) = each( $prices );

This line uses each() to take the current element from $prices, return it as an array, and make the next element current. It also uses list() to turn the 0 and 1 elements from the array returned by each() into two new variables called $product and $price.

You can loop through the entire $prices array, echoing the contents using this short script:

while ( list( $product, $price ) = each( $prices ) )
 echo "$product - $price<br />";

It has the same output as the previous script but is easier to read because list() allows you to assign names to the variables.

When you are using each(), note that the array keeps track of the current element. If you want to use the array twice in the same script, you need to set the current element back to the start of the array using the function reset(). To loop through the prices array again, you type the following:

reset($prices);
while ( list( $product, $price ) = each( $prices ) )
 echo "$product - $price<br />";

This code sets the current element back to the start of the array and allows you to go through again.

  • + Share This
  • 🔖 Save To Your Account