Home Logo
Show Full Menu
Hide Full Menu
 

Arrays

Created: 06/10/10 Views: 1024 myFave Image


In this tutorial we will learn Arrays
An array is like a list, a list that you can keep in a single variable. You are able to call an item from that list using it's name or number, this is an easy way to keep a lot of related information together.

2 easy ways to create an array
  • $arr = array("bre" => "Bread", "mil" => "Milk", "sug" => "Sugar");
  • //In this method we have created the list items name and value, so if you called 'bre' it would result in 'Bread' and so on.
  • OR
  • $arr = array("Bread", "Milk", "Sugar");
  • //In this method we are just creating the list values and we can call them using the id they are automatically given, the id's for the items are just a number, starting at 0.


Ok, so you've seen how to create an array, so lets show you how to call the values.
    Array
  • $arr = array("jam"=>"James", "jac"=>"Jack", "han"=>"Hannah", "amy"=>"Amy");

    Calling
  • print $arr["jac"];
  • print "<br/>";
  • print $arr["han"];

    Result
  • Jack   //item name 'jac' in the array
  • Hannah   //item name 'han' in the array

    Array
  • $arr = array("James","Ben","Jack","Sarah","Hannah","Amy");

    Calling
  • print $arr[0];
  • print "<br/>";
  • print $arr[5];

    Result
  • James   //position 0 in the array
  • Amy   //position 5 in the array

Give it a go yourself!

So you should be able to recognise that you call an item from the array by using square brackets with the items id or name inbetween.


next tutorial: Include

There are no comments