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: 

<?php $colors = array("Red", "Green", "Blue"); echo "I like " . $colors[0] . ", " . $colors[1] . " and " . $colors[2] . "."; ?>
Here is an example that creates an array named $pets, and adds some values to it and then echo’s out those values specifically:

Example: 

<?php $pets = array("Dog", "Cat", "Rabbit"); echo $pets[0]. " are loyal <br>"; echo $pets[1]. " are most loved <br>"; echo $pets[2]. " have great bond with their owner <br>"; ?>

PHP Indexed Arrays


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: 

<?php $colors = array("Red", "Green", "Blue"); $length = count($colors); for($a = 0; $a < $length; $a++) { echo $colors[$a]; echo " <br>"; } ?>

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:

php indexed arrays

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():

Example: 

<?php $pets = array("Dogs", "Rabbit", "Cats"); $length = count($pets); for($a = 0; $a < $length; $a++) { echo $pets[$a]. " is a cute pet <br>"; } ?>
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *