问题很简单。我有一个foreach循环在我的代码:
foreach($array as $element) {
//code
}
在这个循环中,当我们在第一次或最后一次迭代时,我希望做出不同的反应。
如何做到这一点?
问题很简单。我有一个foreach循环在我的代码:
foreach($array as $element) {
//code
}
在这个循环中,当我们在第一次或最后一次迭代时,我希望做出不同的反应。
如何做到这一点?
当前回答
不确定是否还有必要。但是下面的解决方案应该与迭代器一起工作,并且不需要计数。
<?php
foreach_first_last(array(), function ($key, $value, $step, $first, $last) {
echo intval($first), ' ', intval($last), ' ', $step, ' ', $value, PHP_EOL;
});
foreach_first_last(array('aa'), function ($key, $value, $step, $first, $last) {
echo intval($first), ' ', intval($last), ' ', $step, ' ', $value, PHP_EOL;
});
echo PHP_EOL;
foreach_first_last(array('aa', 'bb', 'cc'), function ($key, $value, $step, $first, $last) {
echo intval($first), ' ', intval($last), ' ', $step, ' ', $value, PHP_EOL;
});
echo PHP_EOL;
function foreach_first_last($array, $cb)
{
$next = false;
$current = false;
reset($array);
for ($step = 0; true; ++$step) {
$current = $next;
$next = each($array);
$last = ($next === false || $next === null);
if ($step > 0) {
$first = $step == 1;
list ($key, $value) = $current;
if (call_user_func($cb, $key, $value, $step, $first, $last) === false) {
break;
}
}
if ($last) {
break;
}
}
}
其他回答
对于SQL查询生成脚本,或任何对第一个或最后一个元素执行不同操作的脚本,避免使用不必要的变量检查要快得多(几乎快两倍)。
目前公认的解决方案使用循环和循环内的检查,将使every_single_iteration,正确的(快速)方法如下:
$numItems = count($arr);
$i=0;
$firstitem=$arr[0];
$i++;
while($i<$numItems-1){
$some_item=$arr[$i];
$i++;
}
$last_item=$arr[$i];
$i++;
一个自制的基准测试显示如下:
Test1: 100000次模型morg
时间:1869.3430423737毫秒
Test2:模型运行100000次
时间:3235.6359958649毫秒
因此,很明显,支票的成本很高,当然,你添加的变量越多,情况就越糟糕;)
试试这个:
function children( &$parents, $parent, $selected ){
if ($parents[$parent]){
$list = '<ul>';
$counter = count($parents[$parent]);
$class = array('first');
foreach ($parents[$parent] as $child){
if ($child['id'] == $selected) $class[] = 'active';
if (!--$counter) $class[] = 'last';
$list .= '<li class="' . implode(' ', $class) . '"><div><a href="]?id=' . $child['id'] . '" alt="' . $child['name'] . '">' . $child['name'] . '</a></div></li>';
$class = array();
$list .= children($parents, $child['id'], $selected);
}
$list .= '</ul>';
return $list;
}
}
$output .= children( $parents, 0, $p_industry_id);
要找到第一个,可以这样做:
$first = true;
foreach ( $obj as $value )
{
if ( $first )
{
// do something
$first = false; //in order not to get into the if statement for the next loops
}
else
{
// do something else for all loops except the first
}
}
1:为什么不用简单的for语句呢?假设你使用的是一个真实的数组而不是Iterator,你可以很容易地检查counter变量是0还是比元素的总数少1。在我看来,这是最干净、最容易理解的解决方案……
$array = array( ... );
$count = count( $array );
for ( $i = 0; $i < $count; $i++ )
{
$current = $array[ $i ];
if ( $i == 0 )
{
// process first element
}
if ( $i == $count - 1 )
{
// process last element
}
}
2:你应该考虑使用嵌套集来存储你的树结构。此外,您可以通过使用递归函数来改进整个过程。
如果您更喜欢不需要在循环外初始化计数器的解决方案,那么您可以将当前迭代键与告诉您数组的最后/第一个键的函数进行比较。
PHP 7.3及更新版本:
foreach ($array as $key => $element) {
if ($key === array_key_first($array)) {
echo 'FIRST ELEMENT!';
}
if ($key === array_key_last($array)) {
echo 'LAST ELEMENT!';
}
}
PHP 7.2及以上版本:
PHP 7.2已经是EOL(生命终结),所以这里只是作为历史参考。避免使用。
foreach ($array as $key => $element) {
reset($array);
if ($key === key($array)) {
echo 'FIRST ELEMENT!';
}
end($array);
if ($key === key($array)) {
echo 'LAST ELEMENT!';
}
}