Services
Blog
Map
Contact Me
Rss
About
An array is list of values that can be referenced with a single variable. This is achieved by using an index to reference individual array elements. Either a number or a word can be an index. Number indices start with zero.
Arrays are named in exactly the same way as variables, but with square brackets after the array name. In the following example, words "cake," "bread," and "carrot" are added to an array.
$goods[]= "cake"; $goods[]= "bread"; $goods[]= "carrot";
A particular array element is referenced by specifying its index in square brackets. For example, the following code displays the contents of the zero element, which is "cake":
print("<P> $goods[0]");
When storing values in the array, we did not specify into which array cells they were to be placed. Thus, they were stored in sequential locations starting with zero. You can also specify the array cell to store a value explicitly. For example:
$goods[0]= "cake"; $goods[1]= "bread"; $goods[2]= "carrot";
This way is more convenient, as here you control which value goes into which array cell. Especially since the cells can be specified in any order you wish. For example:
$goods[3]= "cake"; $goods[9]= "bread"; $goods[2]= "carrot";
When a new item is added to an existing array without specifying the cell into which to place it, it is automatically placed into the cell following the highest index cell.
Thus, the value in the following code:
$goods[]= "potatoes";
will be placed into cell 10 of the array we created in the previous example.
The following example shows how arrays with character instead of numerical indices are created:
$goods[ca]= "cake"; $goods["b"]= "bread"; $goods["cr"]= "carrot"; echo($goods["b"]);
As you can see, there is no big difference between the numerical and literal indices.
A more intellectual way of creating arrays is to use the array() function. For example, as follows:
$goods = array("cake", "bread", "carrot");
In this case, the array variable has the same format as any other variable. It is set to the array created by the array() function, which lists the array elements in the parenthesis. These elements will be stored in cells with indices from 0 to 2.
The array() function can also create arrays with literal indices. For example:
$goods = array("ca" => "cake", "b" => "bread", "cr" => "carrot")
In this case, array elements are assigned to the corresponding cells using the "=>" assignment operator.
Because the array indices are not ordered, we have to create some loop for viewing all array items. The foreach loop, which we have not considered yet, is intended specifically for cycling through array elements. Its general format is as follows:
foreach (array as [$key => ] $value) statement;
If the $key variable specified (the variable can have any name), this variable can be used in the loop body to reference an element index. The variable $value is used to reference an array element.
The following code displays the indices of the $goods array and the values of their corresponding elements:
foreach($goods as $Ind => $Val)
{
print("<P> index: $Ind <BR>value: $Val");
}
There are quite a few array handling functions in PHP; for now, however, we will only consider briefly the most frequently used one: the count() function. The count() function takes an array name as the parameter and returns the number of elements in the array. For example, the following code displays the number of elements in the $goods array:
echo(count($goods));