在数组迭代期间检查,如果当前元素是最后一个元素

请帮我把这个伪代码翻译成真正的php代码:

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

编辑:数组可能有数字或string键

你可以使用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

正如@Mijoja所指出的那样,如果在数组中有多个相同的值,那么上面的代码可能会有问题。 下面是它的修复。

 $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

我发现由@onteria_解决scheme是更好,然后我已经回答,因为它不修改数组内部指针,我正在更新答案来匹配他的答案。

 $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. } } 

谢谢@onteria_

这总是为我做的伎俩

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

编辑30/04/15

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

避免@Warren Sergent提到的E_STRICT警告

 $array_keys = array_keys($array); $last_key = end($array_keys); 
 $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"; } } 

由于array_pop在由array_keys创build的临时数组上工作,所以根本不会修改原始数组。

 $ php test.php test1 -> foo test2 -> bar test3 -> baz 0 -> foo 1 -> bar 2 -> baz 

我的解决scheme,也很简单..

 $array = [...]; $last = count($array) - 1; foreach($array as $index => $value) { if($index == $last) // this is last array else // this is not last array } 

如果项目按数字sorting,则使用key()函数确定当前项目的索引并将其与长度进行比较。 您必须使用next()或prev()来循环while循环中的项目而不是for循环:

 $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 } 
 $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 

我知道这是旧的,使用SPL迭代器可能只是一个矫枉过正,但无论如何,这里的另一个解决scheme:

 $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; } }