Indexed array stores value in numeric index always starts with 0, to n-1, where n is number of elements – 1. These index are always integer in indexed arrays. In indexed array, value can be of any data type like integer, float, string, Boolean etc.

Create Indexed Array

To create indexed array use PHP array() function

Syntax

array_name = array(item1,item2,item3,item4);

Example

<?php

 $arr = array(12, 33, 44, 55, 99);

?>

Access indexed array values

To access array values there are two ways

  1. Loop
  2. with Index number directly

Loop

With loop, all elements or values of array can access at once.

<?php

$arr = array(12, 33, 44, 55, 99);
 
for($i = 0; $i < count($arr); $i++){
       echo $arr[i];
}

?>

for, while, do-while any loop can be used to access array values.

count() returns total number of elements present in an array.

With Index number

<?php

 $arr = array(12,33,44,55,99);
 
       echo $arr[2];
       echo $arr[4];
       echo $arr[0];
?>

Accessing array elements with index number gives value of that particular index. Accessing array elements is also called traversing.