What is an Array?
A variable is a storage area holding a
number or text. The problem is, a variable will hold only one value. An array
is a special variable, which can store multiple values in one single variable. In
PHP, there are three kinds of arrays:
- Numeric array - An array with a numeric index
- Associative array - An array where each ID key is associated with a value
- Multidimensional array - An array containing one or more arrays
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("Saab","Volvo","BMW","Toyota");
2. In the following example we assign
the index manually:
$cars[0]="Saab";$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";