Defining Arrays with PHP

I’m going to show you a couple cool tricks you can use for defining arrays like this.

Notice that this type of array uses a consecutive list of integers. When using strings or nonconsecutive integers, these tricks won’t work, but when using similar lists these tricks are great.

Skip the Array Key Definition

The first trick is to skip defining the array key. When you begin defining the array with the short syntax and skip the "key" => part (using only a list of values), you get an array with integer indices starting at 0. For example, say you did this:

$testarray = array('a','b','c','d','e'),

You get an array with these elements:

$testarray[0]='a';
$testarray[1]='b';
$testarray[2]='c';
$testarray[3]='d';
$testarray[4]='e';

The declarations are identical. Note that because we start counting with 0, a five-element array ends at array key 4.

If you want to do this without using short syntax, there is a method for that also:

$testarray = array();
$testarray[]='a';
$testarray[]='b';
$testarray[]='c';
$testarray[]='d';
$testarray[]='e';

In this case, we just leave the key absolutely blank, and it assigns the next integer value to the array. Let’s take a look at an example. The following is code from phpft10-03.php and it appears onscreen like Figure 10.3.

Figure 10.3. Creating array elements without keys in phpft10-03.php.


<?php

$newarray = array("January", "February", "March", "April", "May", "June",
  "July"
, "August", "September", "October" , "November", "December");
echo $newarray[0] . '<br /> ';
echo $newarray[1] . '<br /> ';
echo $newarray[2] . '<br /> ';
echo $newarray[3] . '<br /> ';
echo $newarray[4] . '<br /> ';
echo $newarray[5] . '<br /> ';
echo $newarray[6] . '<br /> ';
echo $newarray[7] . '<br /> ';
echo $newarray[8] . '<br /> ';
echo $newarray[9] . '<br /> ';
echo $newarray[10] . '<br /> ';
echo $newarray[11] . '<br /> ';
?>

Note that in this case, the months started with 0 and ended with 12. Many times in programs it is easier to think of calendar months as 112, rather than 011. If you do this, you should probably create a blank month at month 0 and start January off at array element 1.

Numeric indices for your array are really useful when you are going to loop through all of the data and manipulate them. For example, in the golf example at the beginning of the chapter, we stored the hole number as the key and the score on that hole as a value. If we loop through all the holes from 118, we would get the final full score! String indices are better for describing what is stored in the array, such as with $_GET[] arrays. You have string describers, so it is easier to see what is stored in each element.

Use a Loop

Now there is one more trick involved with basic array creation and display. Notice how hard it is to type out all those echo statements? Wouldn’t it be easier if you could loop through the array and display them all?

There is a special type of loop that does this, but it is more powerful and we will be going over it later on in this chapter. For now, let’s use a basic for loop to make this program a lot easier to write. The following code is from phpft10-04. Check out Figure 10.4, which shows what this array looks like on the screen.

Figure 10.4. Creating array elements with a for loop phpft10-04.php.


<?php

$newarray = array("January", "February", "March", "April", "May", "June",
  "July"
, "August", "September", "October" , "November", "December");

for ($i = 0; $i <= 11; $i++)
{
       echo $newarray[$i] . '<br /> ';
}

?>

See how this program works? A for loop goes through all the indices and elements of an array, and displays them on the screen. It’s a lot easier than actually writing out each element manually.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.184.90