Arrays

 

Introduction.  We are moving through the standard tools of web programming and programming at quite a clip.  We can do this because most of the concepts are common to almost all other languages and you have seen them in other settings before.  Even much of the syntax has been common to some other languages.  While a few things we have covered only have commonalities in other scripting languages, they are likely to look quite different on the surface.  I won't go into these differences at present because I don't know what other languages I have seen before.  But I think it is very important to be able to reason and problem solve in ways that aren't wrapped up in specific syntax, though it is important to know about different languages strengths and weaknesses.

Well now, after this little speech, we need to get back to the task at hand.  The next topic we will introduce so that we can use it in more depth later is arrays.  Some reasons for making use of arrays follow,

  • a way to store and organize numbers in a matrix
  • a way to store and organize certain information that you want to have together in more temporary ways, but yet it is not important or elaborate enough to put it in a database table
  • access information by numerical indices
  • access information by an associative term

Obviously, this list can continue to grow ad infinitum.

Arrays end up being unusually important in PHP for a number of reasons, some of which follow.

  • when information is posted from a form to a processing page it can be accessed in an array

We will give some simple examples of arrays.  The pages following this one will survey the syntax involved in using them and present programming examples.

Some Simple Examples.  The elements in an array are accessed by some sort of numbering scheme.  For example you could have an array of dates called DentistVisitDates that contains the dates of the last 10 times you visited your dentist.  It might be the case that

 

DentistVisitDates[0] = 11/3/95
DentistVisitDates[1] = 6/27/96
. . .
DentistVisitDates[9] = 5/10/00

 

Notice several things about the list.  

  • The index begins at 0 and ends with 9.  Array indices might begin with 1 and end with 10.

  • The most recent dates have the higher indices, a totally unnecessary relationship.  

  • We have used a short date format, mm/dd/yy to represent the dates.  

  • There is a single index.

As another example you might have an array of the names of the 9 planets in our solar system.

 

Planet[0] = Mercury
Planet[1] = Venus
Planet[2] = Earth
. . .
Planet[8] = Pluto

 

Assuming I remember the names and positions of the planets I have listed them in order of their distance from the sun.

Arrays can have more than one index to denote a location in the array.  The number of independent indices used is considered to give the array a particular number of dimensions.  You can have one-dimensional, two-dimensional or as many dimensional arrays as you want.  It is always good programming/development practice to try to have some sort of appropriate relationship between the indices and the data you are trying to put in the array.

Now consider augmenting the array of the Planets to contain the names of each planet's moons.  Since it is my understanding that Jupiter has the most moons and there are currently considered to be 16 of them we need to be able to associate at least 16 additional names with each element in our array.  We will now define a new array called PlanetMoon with 9 rows and 17 columns to make sure we can contain the name of the planets and all their satellites.  Regardless of the fact that this is not a particularly effective use of storage because of all the blank entries we will now fill in some of the elements.  Though, if the two dimensional arrays are developed as arrays of arrays this will not necessarily happen.

 

PlanetMoon[0,0] = Mercury
PlanetMoon[1,0] = Venus
PlanetMoon[2,0] = Earth PlanetMoon[2,1] = Moon
PlanetMoon[3,0] = Mars PlanetMoon[3,1] = Phobos PlanetMoon[3,2] = Deimos

. . .

 

I think that you can see how this array could be filled out with the help of a good astronomy reference.

But now what if we want to include all of the smaller satellites, either artificial or natural, that each of these planets/moons have?  We could realistically add another dimension to our array to list each of those that meet certain specified criteria.  This would just require some more bookkeeping and organization.