PHP Tutorial
These arrays can store numbers, strings and any object but their index will be represented by numbers. By default array index starts from zero.
There are two ways to create indexed arrays:
//index can be assigned automatically (index always starts at 0)
$variable = array("value1", "value2",.... "valueN");
//the index can be assigned manually
$variable[0] = "value1";
$variable[1] = "value2";
$variable[2] = "value3";
.
.
$variable[N] = "valueN";
Let's put these syntax into real use.
Following is the example showing how to create and access numeric arrays.
<?php
/* First method to create array. */
$numbers = array( 1, 2, 3, 4, 5);
foreach( $numbers as $value ) {
echo "Value is $value <br />";
}
/* Second method to create array. */
$numbers[0] = "one";
$numbers[1] = "two";
$numbers[2] = "three";
$numbers[3] = "four";
$numbers[4] = "five";
foreach( $numbers as $value ) {
echo "Value is $value <br />";
}
?>
This will produce the following result −
Value is 1
Value is 2
Value is 3
Value is 4
Value is 5
Value is one
Value is two
Value is three
Value is four
Value is five