PHP Numeric Array

PHP Numeric Array is an array in which numbers, strings, and any other object can be stored, but their indexes will be represented by numbers. Each element of the array is represented by an index number that begins with 0.

PHP Numeric Array Definition

There are two ways of defining PHP numeric array:

1.

$colour=array("Black","White","Red");  

2.

$colour[0]="Black";  
$colour[1]="White"";  
$colour[2]="Red";  

The example below shows how to create and access numeric arrays.

<h2> Example of Numeric Array</h2>
<?php  
$colour=array("Black","White","Red");  
echo "Colour:  $colour[0], $colour[1] and $colour[2]";  
?>

Count Length PHP Numeric Array

The count() method in PHP returns the length of an array.

<h2>Count Length of Numeric Array</h2>
<?php  
$colour=array("Black","White","Red","Blue");  
echo "Colour:  $colour[0], $colour[1], $colour[2] and $colour[3] </br> </br>" ;  
echo "Total array elements are: ".count($colour);
?>  

Traversing PHP Numeric Array

In PHP array can be traversed easily by using foreach loop. Let’s look at a simple example of traversing the entire PHP array.

<h2> Traversing Numeric Array</h2>
<?php  
$colour=array("Black","Blue","Red");  
foreach( $colour as $c )  
{  
  echo "Colour is: $c<br />";  
}  
?>