<? Information in Sets with Arrays?>

Arrays

An array is an alternative way to store a group of like variables. Instead of naming each and every variable, we can group these variables together, and with the help of the "for" statement, we can loop through the variables to look for a match to our search, etc. Examples of variables that may be good candidates for arrays could be:

The names of the Presidents

The titles of books you sell

All the items in your store

All the chickens in a henhouse

We use arrays when we don't want to bother naming each variable, or alternatively, because we CAN'T as there may either be too many, or more likely we don't wish to control how many there may ultimately be of any particular variable.

What if I wrote a program that accomodated a total of 10 possible sandwiches in a delicatessen. If the owners came up with a new sandwich, would I have to re-write my code? Not if I created an array to store the sandwich variables!

Arrays can be created all at one time, in one line:

$aFruit = array("bananas","apples","oranges");

And each item in the array can be accessed by looping through a "for" statement:

for($x = 0; $x < count($aFruit); $x++)
{
    print $aFruit[$x] . "<br>";
}

Note that the array of "fruit" here is accessed by the array "element" number. Arrays start at zero, and count up as each item is added, so a 3 element array gets to the number 2. The items of the array can be accessed in this manner by calling the array, and appending the element number.

Arrays can also be created one line at a time:

$aCheese = array();
$aCheese[0] = "cheddar";
$aCheese[1] = "swiss";
$aCheese[2] = "limburger";

In this case the array of "cheeses" is created first, but has no elements. The array grows by one, each time an item is added to the array. Arrays can be accessed by another version of the for loop, called foreach:

foreach ($aCheese as $cheese)
{
    print $cheese . "<br>";
}

With the foreach, you do not need to specify the element number of the array at all. A new variable is created inside the foreach ($cheese) and then the new variable is called out for each element of the array.

Associative Arrays

Arrays in PHP can be associative, which means that the element (also called a "key") can be a word, rather than a number. This can be helpful when we want to easily see what each item of a group of associated variables can contain. Here is an example:

$aPicnic = array('Sandwich'=>'Turkey','Drink'=>'Pop','Dessert'=>'Cupcake');

The array is created with a new symbol, =>, which indicates adding the value (Turkey) to a key (Sandwich), for example. Here the array is accessed using a foreach:

print "<p>My Picnic basket includes: <br>";
foreach($aPicnic as $myKey => $myValue)
{
    print $myKey. ": ".$myValue."<br>";
}
print "</p>";

This type of array actually makes more sense calling out the key names inside a sentence:

print "<p>In my picnic basket, I have a ". $aPicnic['Sandwich']. " sandwich, ";
print "a ". $aPicnic['Drink']. " to drink, and a " . $aPicnic['Dessert']. " for dessert!<br></p>";

Array Examples

Below are some example files that help us further understand the nature of arrays. First is the example file as spelled out in the text above:

arrayPractice.php View Code

Next is an example where you can insert values into text boxes and process a postback to the form page, and the items you input (colors, clothes, emotions) are fed back via arrays to create nonsense sentences:

arrayTest.php View Code

The next example displays the "sorting" capabilities of arrays. You input comma separated strings of words, names, etc. and the page demonstrates how PHP can sort and shuffle the arrays that it creates out of the string you entered:

arraySort.php View Code

 

Built In Arrays

PHP provides data we can use while programming in the form of arrays as well.  Most of the superglobals like $_POST, $_GET, $_SESSION, $_SERVER and $_COOKIES are presented to use in the form of associative arrays:

print $_POST['FirstName'];

Note the array of POST data is provided the string that is the key in the associative array.  Later we'll loop through all the items in such an array, if we wish.

Print this Page Back To Top

© 2000- 2010 newMANIC INC, All rights reserved