I have an array of objects, any array in php. How do i skip the last element in the foreach iteration?
-
5`if $element == end($myarray) break` – sashkello Feb 20 '14 at 06:30
-
use array_pop() before foreach – PravinS Feb 20 '14 at 06:30
-
@sashkello That will give undesired results if there are duplicate elements in the array, or when there are elements that evaluate to the same value as the last one. – Decent Dabbler Feb 20 '14 at 06:34
-
2Well, why is there an extra element at the end of your array in the first place? – Ja͢ck Feb 20 '14 at 06:36
-
@fireeyedboy Ah, true, you can otherwise use `end` on `array_keys`. – sashkello Feb 20 '14 at 06:37
5 Answers
Use a variable to track how many elements have been iterated so far and cut the loop when it reaches the end:
$count = count($array);
foreach ($array as $key => $val) {
if (--$count <= 0) {
break;
}
echo "$key = $val\n";
}
If you don't care about memory, you can iterate over a shortened copy of the array:
foreach (array_slice($array, 0, count($array) - 1) as $key => $val) {
echo "$key = $val\n";
}

- 170,779
- 38
- 263
- 309
-
i don't know why it felt like it was a bad question, but this was certainly what i was asking about. I want to loop through everything but exclude the last element in the array.. Thanks – muffin Feb 20 '14 at 06:46
-
2@muffin The question is really why that last element should be excluded in the first place ... if there's something special about that value, perhaps it shouldn't be there. – Ja͢ck Feb 20 '14 at 06:47
-
Both approaches worked fine, no errors... very self explanatory. Thank You. – Samuel Ramzan Jan 20 '21 at 03:53
-
There's various ways to do this.
If your array is a sequentially zero-indexed array, you could do:
for( $i = 0, $ilen = count( $array ) - 1; $i < $ilen; $i++ )
{
$value = $array[ $i ];
/* do something with $value */
}
If your array is an associative array, or otherwise not sequentially zero-indexed, you could do:
$i = 0;
$ilen = count( $array );
foreach( $array as $key => $value )
{
if( ++$i == $ilen ) break;
/* do something with $value */
}

- 22,532
- 8
- 74
- 106
-
-
@muffin You're welcome. As you can see from other answers, there are lots of ways to achieve what you were after. – Decent Dabbler Feb 20 '14 at 06:53
-
You have a typo in for-loop. Should be a semicolon instead of a comma. – Irfanullah Jan Jun 06 '19 at 06:37
-
1@erfan It's not a typo. I'm initializing two variables (separated by a comma) in the first part of the for loop. Each expression in `for( expr1; expr2; expr3 )` can [be empty or contain multiple expressions separated by commas](https://www.php.net/manual/control-structures.for.php). – Decent Dabbler Jun 10 '19 at 16:32
-
If you don't want to delete the last array entry with pop, you could skip it like this
$array = array('v1','v2','v3',...)
$counter = 1;
foreach($array as $value)
{
//do your thing in loop
if($counter == count($array)) continue; // this will skip to next iteration if last element encountered.
$counter++;
}
What you are trying to do will defeat the purpose of foreach loop. It is meant to loop through the entire array and make our job easy.
for ex: You can get the array size using COUNT function in php and then can use for loop and set the limit to arraysize-2, so the last array will be omitted

- 8,028
- 12
- 48
- 89
$count = count($array);
$i=0;
foreach ($arr as &$value)
{
$i++;
if($i==($count-1))
{
echo 'skip';
}
else
{
echo $value;
}
}

- 2,119
- 3
- 19
- 32