Friday, August 29, 2025
HomeLanguagesHow to find the index of foreach loop in PHP ?

How to find the index of foreach loop in PHP ?

The foreach construct provides the easiest way to iterate the array elements. It works on array and objects both. The foreach loop though iterates over an array of elements, the execution is simplified and finishes the loop in less time comparatively. It allocates temporary memory for index iterations which makes the overall system to redundant its performance in terms of memory allocation. An error will be issued, if we will try to use foreach loop with uninitialized variable or a variable with different data types.

Syntax:

foreach( $array as $element ) {
    // PHP Code to be executed
}

or

foreach( $array as $key =>> $element) {
    // PHP Code to be executed
}

Return Values:

  • $key: This variable holds the key index of current element/
  • $value: In each iteration, the current value of array element is assigned to the $value variable.

Program 1:




<?php
  
// Declare an array
$arr = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
  
// Iterate through the array using foreach
// construct and store the key and its value
  
// Use foreach loop to display the
// key of allelements
foreach ($arr as $key => $value) {
    echo "key = " . $key . ", value = "
        . $value . "\n";
}
  
?>


Output:

key = 0, value = 1
key = 1, value = 2
key = 2, value = 3
key = 3, value = 4
key = 4, value = 5
key = 5, value = 6
key = 6, value = 7
key = 7, value = 8
key = 8, value = 9
key = 9, value = 10

Program 2:




<?php
  
// Declare an array
$arr = array(
    "a" => "Welcome",
    "b" => "to",
    "d" => "neveropen"
);
  
// Iterate through the array using foreach
// construct and store the key and its value
  
// Use foreach loop to display the
// key of allelements
foreach ($arr as $key => $value) {
    echo "key = " . $key . ", value = "
        . $value . "\n";
}
  
?>


Output:

key = a, value = 1
key = b, value = 2
key = c, value = 3
key = d, value = 4

Program 3:




<?php
  
// Declare a multi-dimensional array
$arr = array(
    array(1, 2, 3), 
    array(4, 5, 6),
    array(7, 8, 9)
);
  
// Iterate through the array using foreach
// construct and store the key and its value
  
// Use foreach loop to display the
// key of allelements
foreach ($arr as $keyOut => $out) {
    foreach($out as $keyIn => $value) {
        echo "key = (" . $keyOut . ", " . $keyIn
             . "), value = " . $value . "\n";    
    }
}
  
?>


Output:

key = (0, 0), value = 1
key = (0, 1), value = 2
key = (0, 2), value = 3
key = (1, 0), value = 4
key = (1, 1), value = 5
key = (1, 2), value = 6
key = (2, 0), value = 7
key = (2, 1), value = 8
key = (2, 2), value = 9
Previous article
Next article
RELATED ARTICLES

Most Popular

Dominic
32249 POSTS0 COMMENTS
Milvus
80 POSTS0 COMMENTS
Nango Kala
6617 POSTS0 COMMENTS
Nicole Veronica
11789 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11838 POSTS0 COMMENTS
Shaida Kate Naidoo
6731 POSTS0 COMMENTS
Ted Musemwa
7011 POSTS0 COMMENTS
Thapelo Manthata
6688 POSTS0 COMMENTS
Umr Jansen
6701 POSTS0 COMMENTS