请帮我把这个伪代码翻译成真正的php代码:
foreach ($arr as $k => $v)
if ( THIS IS NOT THE LAST ELEMENT IN THE ARRAY)
doSomething();
编辑:数组可能有数字键或字符串键
您可以使用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';
}
}
更新1
正如@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.
}
}
更新2
我发现@onteria_的解决方案比我回答的更好,因为它不会修改数组的内部指针,我正在更新答案以匹配他的答案。
$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_
更新3
正如@CGundlach所指出的,PHP 7.3引入了array_key_last
,如果您使用的是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';
}
}
这对我来说总是有用的
foreach($array as $key => $value) {
if (end(array_keys($array)) == $key)
// Last key reached
}
编辑2015年4月30日
$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
创建的临时数组上工作,因此它根本不会修改原始数组。
$ php test.php
test1 -> foo
test2 -> bar
test3 -> baz
0 -> foo
1 -> bar
2 -> baz
为什么不使用这个非常简单的方法:
$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
}
}
我知道这是旧的,使用SPL迭代器可能只是一种过度使用,但无论如何,这里还有另一个解决方案:
$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;
}
}
我的解决方案也很简单。。
$array = [...];
$last = count($array) - 1;
foreach($array as $index => $value)
{
if($index == $last)
// this is last array
else
// this is not last array
}
如果项目是按数字顺序排列的,请使用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
}
使用array_keys
函数获取密钥的简单方法,并且只获取反转数组的第一个密钥[0]
,这是最后一个密钥。
$array = array('a' => 1,'b' => 2,'c' => 3);
$last_key = array_keys(array_reverse($array, true))[0];
foreach($array as $key => $value) {
if ($last_key !== $key)
// THIS IS NOT THE LAST ELEMENT IN THE ARRAY doSomething();
}
注意:array_reverse
reverse数组有两个参数,第一个是我们想要反转的数组,第二个是true,以反转数组,保留键的顺序。