一.计数器
$list = array('a','b','c');
$count = 0;
foreach ($list as $c_temp) {
$count++;
if ($count == count($list) - 1) {
} else {
}
}
二.判断key
$list = array('a','b','c');
foreach ($list as $key -> $value) {
if ($key == count($list) - 1) {
} else {
}
}
三.判断value
$list = array('a','b','c');
foreach ($list as $key -> $value) {
if ($value != end($list)) {
} else {
}
}
四.最后一个和其他元素分开处理
$list = array('a','b','c');
$last = array_pop($list);
foreach ($list as $value) {
operate($value);
}
special_operate($last);
参考文献 https://www.cnblogs.com/kenshinobiy/p/4387038.html https://www.zhihu.com/question/20158667
|