shadowline
Arrays Intro << Creating/Accessing Arrays >> Adding/Replacing Arrays
The first step in creating an array is to specify a variable name as an array.
<?php
$pantry = array( );
?>

The next step is to add in the contents for the array to hold.
<?php
$pantry = array(
   "apples",
   "oranges",
   "bananas"
   );
?>

The listed values in an array field is separated by commas.

Now you have to remember how to count. Huh? You mean like 1 2 3 and so on? Yes, exactly. That is how an array will INDEX its contents except for one minor part... it starts counting at zero instead of one. Apples is at index 0, oranges is at index 1, and bananas is at index 2.

As you have seen on the previous page, an array usually looks like this when you are actually working with them :
$pantry[ ];

The square brackets hold an index value. If you wanted to print out oranges, the index value of 1 could be entered in the square brackets.
<?php
echo ("$pantry[1]");
?>

That is ok for the most part, but some people like to start counting at one. To create this effect, we can add our own index values.
<?php
$pantry = array(
   1 => "apples",
   2 => "oranges",
   3 => "bananas"
   );
?>

The list is still comma separated, but now there is a new set of values to use as the INDEX. In a normal equation, the => symbol represents "equal to or greater than". In arrays, it is used to assign values to an index. To print out the value of oranges now would look like :
<?php
echo ("$pantry[2]");
?>

Label names with values can also be used in arrays. This option looks the most similar to having actual variables being contained within the array.

<?php
$pantry = array(
   "box1" => "apples",
   "box2" => "oranges",
   "box3" => "bananas"
   );
?>

Then finding the contents of box2 would look like :
<?php
echo ("$pantry[box2]");
?>
Arrays Intro << Creating/Accessing Arrays >> Adding/Replacing Arrays
shadow

Advertise in the banner along the top, Click here!

Valid XHTML 1.0 Transitional Valid CSS! Text Link Ads
shadow
shadow