Pages

Saturday, August 27, 2011

Array






An array is a special variable, which can store multiple values in one single variable.
If you have a list of items, for example car, storing the cars in single variables could look like this:
$cars1="Honda";
$cars2="Toyota";
$cars3="Marcedes";



An array can hold all your variable values under a single name. And you can access the values by referring to the array name.
Each element in the array has its own index so that it can be easily accessed.
there are three kind of PHP arrays:
  • Numeric array
  • Associative array
  • Multidimensional array

Numeric Arrays

A numeric array stores each array element with a numeric index.
There are two methods to create a numeric array.
1. In the following example the index are automatically assigned (the index starts at 0):
$cars=array("Honda","Toyota","Marcedes");
2. In the following example i assign the index manually:
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";




Associative Arrays

An associative array, each ID key is associated with a value.
For example i use an array to assign old to the different cars:
$old = array("Honda"=>32, "Toyota"=>30, "Marcedes"=>34);






Multidimensional Arrays

In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on.









No comments:

Post a Comment