Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking during array iteration, if the current element is the last element

Tags:

arrays

php

Please help me to translate this pseudo-code to real php code:

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

Edit: the array may have numerical or string keys

like image 356
shealtiel Avatar asked May 23 '11 01:05

shealtiel


People also ask

How do you check if an item is last in an array?

To get the last item without knowing beforehand how many items it contains, you can use the length property to determine it, and since the array count starts at 0, you can pick the last item by referencing the <array>. length - 1 item.

How do you check the last element in foreach?

Use count() to determine the total length of an array. The iteration of the counter was placed at the bottom of the foreach() loop - $x++; to execute the condition to get the first item. To get the last item, check if the $x is equal to the total length of the array. If true , then it gets the last item.

Is last element of array PHP?

You can use the end() function in PHP to get the last element of any PHP array. It will set the internal pointer to the last element of the array and return its value.


2 Answers

you can use PHP's 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 pointed out by @Mijoja the above could will have problem if you have same value multiple times in array. below is the fix for it.

$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 solution by @onteria_ to be better then what i have answered since it does not modify arrays internal pointer, i am updating the answer to match 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.     } } 

Thank you @onteria_

Update3

As pointed by @CGundlach PHP 7.3 introduced array_key_last which seems much better option if you are using 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';      } } 
like image 144
Ibrahim Azhar Armar Avatar answered Sep 18 '22 15:09

Ibrahim Azhar Armar


This 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); 
like image 33
Richard Merchant Avatar answered Sep 17 '22 15:09

Richard Merchant