如何在不更改数组内部指针的情况下获取数组的最后一个元素?
我正在做的是:
while(list($key, $value) = each($array)) {
//do stuff
if($condition) {
//Here I want to check if this is the last element of the array
prev($array);
}
}
所以 end($array)
会把事情搞砸。
最佳答案
很简单,你可以使用:
$lastElementKey = end(array_keys($array));
while(list($key, $value) = each($array)) {
//do stuff
if($key == $lastElementKey) {
//Here I want to check if this is the last element of the array
prev($array);
}
}
关于php - 获取数组的最后一个元素,而不更改其内部指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13891133/