Indexed Arrays

PHP index is represented by number which starts from 0. We can store number, string and object in the PHP array. All PHP array elements are assigned to an index number by default.

 

There are two ways to create indexed arrays:

1. The index can be assigned automatically (index always starts at 0), like this:

Example:

<?php

$fruits = array("Apple", "Orange", "Banana");

// Printing array structure
print_r($fruits);

?>

 

Output:

Array ( [0] => Apple [1] => Orange [2] => Banana )

 

2. The index can be assigned manually:

Example:

<?php

$fruit[0] = "Apple";
$fruit[1] = "Orange";
$fruit[2] = "Banana"; 

print_r($fruit); 

?>

 

Output:

Array ( [0] => Apple [1] => Orange [2] => Banana )