Check during array iteration if the current element is the last element

Please help me translate this pseudocode into real PHP code:

foreach ($arr as $k => $v) if ( THIS IS NOT THE LAST ELEMENT IN THE ARRAY) doSomething(); 

Edit: an array can have numeric or string keys

+69
arrays php
May 23 '11 at 1:42 AM
source share
8 answers

You can use PHP end ()

 $array = array('a' => 1,'b' => 2,'c' => 3); $lastElement = end($array); foreach($array as $k => $v) { echo $v . '<br/>'; if($v == $lastElement) { // 'you can do something here as this condition states it just entered last element of an array'; } } 

Update1

as @Mijoja pointed out, the above can have problems if you have the same value several times in the array. below is the fix.

 $array = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 2); //point to end of the array end($array); //fetch key of the last element of the array. $lastElementKey = key($array); //iterate the array foreach($array as $k => $v) { if($k == $lastElementKey) { //during array iteration this condition states the last element. } } 

Update2

I found that @onteria_'s solution turned out to be better than what I answered, since it does not change the internal pointer of arrays, I am updating the answer so that it matches his answer.

 $array = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 2); // Get array keys $arrayKeys = array_keys($array); // Fetch last array key $lastArrayKey = array_pop($arrayKeys); //iterate array foreach($array as $k => $v) { if($k == $lastArrayKey) { //during array iteration this condition states the last element. } } 

Thanks @onteria_

Update3

As pointed out by @CGundlach, array_key_last was introduced in PHP 7.3, which seems like a much better option if you use PHP> = 7.3

 $array = array('a' => 1,'b' => 2,'c' => 3); $lastKey = array_key_last($array); foreach($array as $k => $v) { echo $v . '<br/>'; if($k == $lastKey) { // 'you can do something here as this condition states it just entered last element of an array'; } } 
+121
May 23 '11 at 1:54 a.m.
source share

It always does the trick for me

 foreach($array as $key => $value) { if (end(array_keys($array)) == $key) // Last key reached } 

Edit 30/04/15

 $last_key = end(array_keys($array)); reset($array); foreach($array as $key => $value) { if ( $key == $last_key) // Last key reached } 

To avoid the E_STRICT warning mentioned by @Warren Sergent

 $array_keys = array_keys($array); $last_key = end($array_keys); 
+20
Oct 31 '14 at 11:22
source share
 $myarray = array( 'test1' => 'foo', 'test2' => 'bar', 'test3' => 'baz', 'test4' => 'waldo' ); $myarray2 = array( 'foo', 'bar', 'baz', 'waldo' ); // Get the last array_key $last = array_pop(array_keys($myarray)); foreach($myarray as $key => $value) { if($key != $last) { echo "$key -> $value\n"; } } // Get the last array_key $last = array_pop(array_keys($myarray2)); foreach($myarray2 as $key => $value) { if($key != $last) { echo "$key -> $value\n"; } } 

Since array_pop works with a temporary array created by array_keys , it does not modify the original array at all.

 $ php test.php test1 -> foo test2 -> bar test3 -> baz 0 -> foo 1 -> bar 2 -> baz 
+12
May 23 '11 at 1:54
source share

Why not this very simple method:

 $i = 0; //a counter to track which element we are at foreach($array as $index => $value) { $i++; if( $i == sizeof($array) ){ //we are at the last element of the array } } 
+4
Feb 24 '18 at 13:14
source share

I know this is old, and using the SPL IPPL might just be a rebuild, but anyway, another solution here:

 $ary = array(1, 2, 3, 4, 'last'); $ary = new ArrayIterator($ary); $ary = new CachingIterator($ary); foreach ($ary as $each) { if (!$ary->hasNext()) { // we chain ArrayIterator and CachingIterator // just to use this `hasNext()` method to see // if this is the last element echo $each; } } 
+2
Jun 29 '16 at 16:56
source share

My solution is also quite simple.

 $array = [...]; $last = count($array) - 1; foreach($array as $index => $value) { if($index == $last) // this is last array else // this is not last array } 
+1
Apr 25 '16 at 4:30
source share

If the elements are numerically ordered, use the key () function to determine the index of the current element and compare it with the length. You will need to use next () or prev () to cycle through the elements in the while loop instead of the for loop:

 $length = sizeOf($arr); while (key(current($arr)) != $length-1) { $v = current($arr); doSomething($v); //do something if not the last item next($myArray); //set pointer to next item } 
0
May 23 '11 at 1:56 a.m.
source share
 $arr = array(1, 'a', 3, 4 => 1, 'b' => 1); foreach ($arr as $key => $val) { echo "{$key} = {$val}" . (end(array_keys($arr))===$key ? '' : ', '); } // output: 0 = 1, 1 = a, 2 = 3, 4 = 1, b = 1 
0
Feb 13 '13 at 22:45
source share



All Articles