Determine the first and last iteration in a foreach loop in PHP?

Given an array of elements and the task is to determine first and last iteration in foreach loop. There are many ways to solve this problem which are listed below:

Method 1: It is the naive method inside foreach loop to find iteration. Use a counter variable and check when the counter value is zero then it is the first iteration and when the counter value is length-1 then it is the last iteration.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP program to get first and
// last iteration
  
// Declare an array and initialize it
$myarray = array( 1, 2, 3, 4, 5, 6 );
  
// Declare a counter variable and
// initialize it with 0
$counter = 0;
  
// Loop starts from here
foreach ($myarray as $item) {
      
    // Check condition if count is 0 then 
    // it is the first iteration
    if( $counter == 0 ) {
          
        // Print the array content
        print( $item );
        print(": First iteration \n");
    }
      
    // Check condition if count is length -1 
    // then it is last iteration
    if( $counter == count( $myarray ) - 1) {
          
        // Print the array content
        print( $item );
        print(": Last iteration");
    }
          
    $counter = $counter + 1;
}
  
?>

chevron_right


Output:

1: First iteration 
6: Last iteration

Method 2: Using reset and end function to find first and last iteration. The reset() function is an inbuilt function in PHP which takes array name as argument and return first element of array. The end() function is an inbuilt function in PHP which takes array name as argument and returns its last element.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP program to get first and
// last iteration
  
// Declare and array and initialize it
$myarray = array( 1, 2, 3, 4, 5, 6 );
  
// Loop starts here
foreach ( $myarray as $item ) {
      
    // Check item and the return value of 
    // reset() function is equal then it 
    // will be the first iteration
    if ( $item === reset( $myarray ) ) {
          
        // Desplay the array element
        print( $item );
        print(": First iteration \n");
    }
      
    // Check if item and return value of 
    // end() function is equal then it
    // will be last iteration
    else if( $item === end( $myarray ) ) {
          
        // Desplay the array element
            print( $item );
        print(": :ast iteration \n");
    }
}
?>

chevron_right


Output:

1: First iteration 
6: :ast iteration

Method 3: The reset() function is used to find the first iteration in foreach loop. When there is no next element is available in an array then it will be the last iteration and it is calculated by next() function.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP program to get first and
// last iteration
  
// Declare an array and initialize it
$myarray = array( 1, 2, 3, 4, 5, 6 );
  
// Assign first array element to variable
$first = reset( $myarray );
  
// Loop starts here
foreach ($myarray as $item) {
      
    // Check if item is equal to first
    // element then it is the first
    // iteration
    if ($item == $first) {
          
        // Display array element
        print( $item );
        print(": First iteration \n");
    }
      
    // If there is no next element 
    // in array then this is last iteration
    if( !next( $myarray ) ) {
          
        // Display array element
        print( $item );
        print(": Last iteration \n");
    }
}
?>

chevron_right


Output:

1: First iteration 
6: Last iteration


My Personal Notes arrow_drop_up

All powers are within you You can do anything and everything Believe in that

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : ManasChhabra2