PHP Numeric Array



PHP Numeric Array :

  1. PHP Numeric array can store numbers, strings and objects.
  2. Index of the Numeric array will be managed by number.
  3. PHP array index starts with zero
  4. array() function is used to create array.

Example #1 : PHP Numeric Array

<html>
<body>
<?php
$numbers = array( 1, 2, 3, 4, 5);
foreach( $numbers as $value )
{
  echo "Value is $value <br />";
}
?>
</body>
</html>

Output :

Value is 1 
Value is 2 
Value is 3 
Value is 4 
Value is 5 

Explanation :

In the above example we have created an array -

$numbers = array( 1, 2, 3, 4, 5);

Now all the array elements can be accessed using the numeric index. i.e -

Element of Array,Value of array element
numbers[0],1
numbers[1],2
numbers[2],3
numbers[3],4
numbers[4],5

Example #2 : PHP Numeric Array of Strings

In this example we have created an array of String but still all the strings are managed by numeric index.

<html>
<body>
<?php
$alpha = array( "AA", "BB", "CC", "DD", "EE");
foreach( $alpha as $value )
{
  echo "Value is $value <br />";
}
?>
</body>
</html>

Output :

Value is AA 
Value is BB 
Value is CC 
Value is DD 
Value is EE

Example #3 : Manually initializing Array Element

<html>
<body>
<?php
$alpha[0] = "one";
$alpha[1] = "two";
$alpha[2] = "three";
$alpha[3] = "four";
$alpha[4] = "five";
foreach( $alpha as $value )
{
  echo "Value is $value <br />";
}
?>
</body>
</html>

Output :

Value is one 
Value is two 
Value is three 
Value is four 
Value is five