Saturday, September 9, 2017

PHP Indexed Array


PHP indexed array is an array which is represented by an index number by default. All elements of array are represented by an index number which starts from 0.

PHP indexed array can store numbers, strings or any object. PHP indexed array is also known as numeric array.

There are two ways to define indexed array:

1st way :
$size=array("One","Two","Three");

2nd way :
$size[0]="One";  
$size[1]="Two";  
$size[2]="Three";

Example -1 :
<?php  
$size=array("One","Two","Three");  
echo "Array Elements are : $size[0], $size[1] and $size[2]";  
?>
Output:
-------------------
Array Elements are : One, Two and Three

Example -2 :
<?php  
$size[0]="One";  
$size[1]="Two";  
$size[2]="Three";  
echo "Array Elements are: $size[0], $size[1] and $size[2]";  
?>
Output :
---------------------
Array Elements are: One, Two and Three

How to traverse each elements of Array ?
Let's see a simple example to traverse all the elements of PHP array.
<?php  
$days=array("Monday","Tuesday","Wednesday");  
foreach( $days as $a )  
{  
  echo "Day is: $a<br />";  
}  
?>
Output:
------------------------
Day is: Monday
Day is: Tuesday
Day is: Wednesday



Count the Length of PHP Indexed Array :
PHP provides count() function which returns length of an array.
<?php  
$days=array("Monday","Tuesday","Wednesday");  
echo count($days);   
?>
Output:
----------------------------
3



No comments:

Post a Comment