About us Privacy Disclaimer Contact us
FAQ Help Advertising Feedback
Home Sitemap Search Donate us

  Home > Computer & I'net > Creating/Accessing Arrays...

   Browse by title articles:
   What is hot:

for loops

Arrays Intro

Creating/Accessing Arrays

Adding/Replacing Arrays

Merging Arrays

Counting Arrays

Website Maintenance: What does it take to manage your website?
Creating/Accessing Arrays
Bad Web Design: Advertising Mistakes
No Computer Sound
Prev articles1234567 8 9101112Next articles



Creating/Accessing Arrays


Computer & I'net articlesCreating/Accessing Arrays

by David Stanley    



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]");
?>



-----------------
Article by David Stanley. Visit his site http://www.htmlite.com. Reprinted with permission.





Holiday spending




  Disclaimer | Privacy | Terms of useCopyright © 2004 Nice2know.com