问题很简单。我有一个foreach循环在我的代码:
foreach($array as $element) {
//code
}
在这个循环中,当我们在第一次或最后一次迭代时,我希望做出不同的反应。
如何做到这一点?
问题很简单。我有一个foreach循环在我的代码:
foreach($array as $element) {
//code
}
在这个循环中,当我们在第一次或最后一次迭代时,我希望做出不同的反应。
如何做到这一点?
当前回答
试试这个:
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);
其他回答
你可以用一个计数器:
$i = 0;
$len = count($array);
foreach ($array as $item) {
if ($i == 0) {
// first
} else if ($i == $len - 1) {
// last
}
// …
$i++;
}
上面的一个更简化的版本,并假设您不使用自定义索引……
$len = count($array);
foreach ($array as $index => $item) {
if ($index == 0) {
// first
} else if ($index == $len - 1) {
// last
}
}
版本2 -因为我已经讨厌使用else除非必要。
$len = count($array);
foreach ($array as $index => $item) {
if ($index == 0) {
// first
// do something
continue;
}
if ($index == $len - 1) {
// last
// do something
continue;
}
}
对于键和值,这也是有效的:
foreach ($array as $key => $value) {
if ($value === end($array)) {
echo "LAST ELEMENT!";
}
}
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:你应该考虑使用嵌套集来存储你的树结构。此外,您可以通过使用递归函数来改进整个过程。
来自@morg的最有效的答案,与foreach不同,它只适用于适当的数组,而不是哈希映射对象。这个答案避免了每次循环迭代都使用条件语句的开销,就像大多数这些答案(包括接受的答案)一样,通过专门处理第一个和最后一个元素,并遍历中间的元素。
array_keys函数可以像foreach一样高效地执行:
$keys = array_keys($arr);
$numItems = count($keys);
$i=0;
$firstItem=$arr[$keys[0]];
# Special handling of the first item goes here
$i++;
while($i<$numItems-1){
$item=$arr[$keys[$i]];
# Handling of regular items
$i++;
}
$lastItem=$arr[$keys[$i]];
# Special handling of the last item goes here
$i++;
我还没有对此进行基准测试,但没有向循环添加逻辑,这是对性能影响最大的地方,因此我怀疑提供有效答案的基准测试非常接近。
如果您想对这类东西进行函数化,我已经在这里尝试了这样一个iterateList函数。不过,如果您非常关心效率,您可能需要对主旨代码进行基准测试。我不确定所有函数调用引入了多少开销。