What Is PHP Array Index? Explained With Example

In PHP language, there are different types of arrays like:

  • Indexed array
  • Associative array
  • Multidimensional array

We use them according to our requirements.

PHP array provides the index number to each of the array elements. These index numbers are used to perform different operations on arrays.

For example:

  • According to the index number of the element, we can get the array elements’ value.
  • We can put some conditions for specific index numbers, and whenever the index number is reached, then the statements inside the condition loop are performed and many more.

PHP Array Index

The index is the unique number provided to each of the array elements in ascending order starting from zero.

Example 1

Suppose we have an indexed array of different shapes as follows:

$shapes = array(“circle”,”rectangle”,”square”,”triangle”);

In the above-indexed array, PHP provides index numbers as follows:

Array element index number

circle – 0
rectangle – 1
square – 2
triangle – 3

We can use these index numbers to print the elements of an array.

Example 2

Suppose we have an array of different shapes, and we want to print an array’s values.

<!DOCTYPE html>
<html>
<body>

<?php
$shapes = array("circle","rectangle","square","triangle");

echo $shapes[0]."<br>";
echo $shapes[1]."<br>";
echo $shapes[2]."<br>";
echo $shapes[3]."<br>";

?>

</body>
</html>

Output

circle
rectangle
square
triangle

Explanation: In the above example, we print the array’s values using array index no.

We can also use the foreach method with index numbers to print the array as follows:

Example 3

<!DOCTYPE html>
<html>
<body>

<?php
$shapes = array("circle","rectangle","square","triangle");

foreach($shapes as $value){
  echo $value."<br>";
}

?>

</body>
</html>

Output

circle
rectangle
square
triangle

Explanation: In the above code, we get the output as same as example 2. Here we use the index number to print the index array.

The PHP index number is also used to check the position of the array element, and based on its position, and we can take different actions as follows:

Example 4

Suppose we have an array of shapes, and we want to print the index number of the array element with its value at the first and last position. We can check the condition as follows:

<!DOCTYPE html>
<html>
<body>

<?php
$shapes = array("circle","rectangle","square","triangle");
$counter = 0;
foreach($shapes as $value){
  // check for first position
  if($counter == 0) {
    echo "First Index:<br>";
    echo "Index number: ".$counter. " value: ".$value."<br>";
  }
  // check for last position
  if($counter == count($shapes) - 1){
    echo "Last Index:<br>";
    echo "Index number: ".$counter. " value: ".$value."<br>";
  }
  $counter +=1;
}

?>

</body>
</html>

Output

First Index:
Index number: 0 value: circle
Last Index:
Index number: 3 value: triangle

Explanation

  • In the above code, we first use the $counter variable to count each array element’s index number.
  • Next, we check the condition for the first and last array elements and, based on the result, write some result statements.
  • Here we use the foreach loop for checking conditions for all array elements and count() function to find the array’s length.
  • In the end, we increment the counter variable after each iteration.

We can also use the index number in an associative array containing keys and values. In this array, we can also check conditions based on the resulting echo of some statements.

Example 5

Suppose we have the following array :

$arr = array(“circle”=>”blue”,”rectangle”=>”red”,”square”=>”black”,”triangle”=>”pink”);

and we want to echo the key and value at first and the last position we can do this as follows:

<!DOCTYPE html>
<html>
<body>

<?php
$shapes = array("circle"=>"blue","rectangle"=>"red","square"=>"red","triangle"=>"pink");
$counter = 0;
foreach($shapes as $key => $value){
  if($counter == 0){
    // check for first position
    echo "First Index:<br>";
    echo "Key = ".$key." and value = ".$value."<br>";
  }
  if($counter == count($shapes) -1){
    // check for last position
    echo "Last Index:<br>";
    echo "Key = ".$key." and value = ".$value."<br>";
  }
  $counter+=1;
}

?>

</body>
</html>

Output

First Index:
Key = circle and value = blue
Last Index:
Key = triangle and value = pink

Explanation: Here, we also use the same counter variable logic in the above associative array.

Conclusion

This is all about the index of the foreach method of PHP. In an indexed array, we can use the foreach loop index for printing the values of an array. We can also check some conditions for array elements, and based on the result, further actions are taken in PHP code.

I hope you found this post fully informative and helpful.

Thank you for reading 🙂

Leave a Reply

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