What is an Indexed Array in PHP?
The concept of PHP indexed array is a collection of elements that can be accessed by using an index or key and are stored in the same memory locations within the same file.
There are a total of n elements in an array, where n equals the number of elements in the array, and the index of an array element starts at 0 and ends at n-1.
In this article, we are about to explore the concept of PHP indexed arrays with examples and find out how we can be able to use them in our code.
PHP Indexed Arrays
PHP Indexed Arrays allows you to create indexed arrays in two ways:
You can automatically assign the index (indices always begin at 0), as shown in the following example:
$colors = array(“Red”, “Green”, “Blue”);
Alternatively, the index may be assigned manually by following these steps:
$colors[0] = “Red”; $colors[1] = “Green”; $colors[2] = “Blue”;
A simple example is given below in which we create an array named $colors, assign three elements to it, and then print out the text that contains the value of the array:
Php Indexed Arrays Example: 
Example: 
Indexed Array & Loops
For example, if you are talking about array indexes, you would be able to use a for loop in order to loop throughout the array and print out all the values in the array, like this:
Example: 
Example Explanation
In above example we have created an indexed array called $colors. It uses a for loop to iterate over its elements and print them to the screen.
The $colors array contains three elements: “Red“, “Green“, and “Blue“. The count function is used to get the number of elements in the array and store it in the variable $length.
The for loop is used to iterate over the elements of the array. The loop starts with the variable $a initialized to 0 and continues as long as $a is less than $length. In each iteration of the loop, the code inside the curly braces {} is executed.
The echo statement inside the loop is used to print the value of the current element of the array to the screen, followed by a line break using the <br> tag. The index of the current element is represented by the loop variable $a.
So, when the loop is executed, the output will be:
This is because the loop iterates over the three elements of the array and prints them one by one, with a line break after each element.
In the following example, we loop through each element of an array pets():