请帮助我将此伪代码转换为真实的php代码:
foreach ($arr as $k => $v)
if ( THIS IS NOT THE LAST ELEMENT IN THE ARRAY)
doSomething();
编辑:数组可能具有数字键或字符串键
Answers:
您可以使用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';
}
}
array_key_last
,该函数还返回数组的最后一个键,而不影响内部指针:secure.php.net/array_key_last。将进一步简化您的解决方案:)
这总是对我有用
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
所以根本不会修改原始数组。
$ php test.php
test1 -> foo
test2 -> bar
test3 -> baz
0 -> foo
1 -> bar
2 -> baz
$first = array_shift(array_keys($myarray2));
我知道这很老了,使用SPL迭代器可能只是一个矫kill过正,但是无论如何,这里还有另一个解决方案:
$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;
}
}
如果项目按数字顺序排序,请使用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
for
循环进行count($arr) - 1
迭代?