PHP - 使用 foreach 抓取第一个元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3957768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:33:58  来源:igfitidea点击:

PHP - Grab the first element using a foreach

phpforeachiteration

提问by MEM

Wondering what would be a good method to get the first iteration on a foreach loop. I want to do something different on the first iteration.

想知道在 foreach 循环中进行第一次迭代的好方法是什么。我想在第一次迭代中做一些不同的事情。

Is a conditional our best option on these cases?

在这些情况下,有条件是我们的最佳选择吗?

回答by poke

Yes, if you are not able to go through the object in a different way (a normal for loop), just use a conditional in this case:

是的,如果您无法以不同的方式(正常的 for 循环)遍历对象,请在这种情况下使用条件:

$first = true;
foreach ( $obj as $value )
{
    if ( $first )
    {
        // do something
        $first = false;
    }
    else
    {
        // do something
    }

    // do something
}

回答by Klinky

Even morer eleganterer:

更优雅更优雅:

foreach($array as $index => $value) {
 if ($index == 0) {
      echo $array[$index];
 }
}

That example only works if you use PHP's built-in array append features/function or manually specify keys in proper numerical order.

该示例仅在您使用 PHP 的内置数组附加功能/函数或以正确的数字顺序手动指定键时才有效。

Here's an approach that is not like the others listed here that should work via the natural order of any PHP array.

这是一种与此处列出的其他方法不同的方法,它应该通过任何 PHP 数组的自然顺序工作。

$first = array_shift($array);
//do stuff with $first

foreach($array as $elem) {
 //do stuff with rest of array elements
}

array_unshift($array, $first);     //return first element to top

回答by Russell Dias

You can simply add a counter to the start, like so:

您可以简单地在开头添加一个计数器,如下所示:

$i = 0;

foreach($arr as $a){
 if($i == 0) {
 //do ze business
 }
 //the rest
 $i++;
}

回答by danielson317

I saw this solution on a blog postin my search result set that brought up this post and I thought it was rather elegant. Though perhaps a bit heavy on processing.

我在搜索结果集中的一篇博客文章中看到了这个解决方案,该文章提出了这篇文章,我认为它相当优雅。虽然在处理上可能有点繁重。

foreach ($array as $element) 
{
    if ($element === reset($array))
        echo 'FIRST ELEMENT!';

    if ($element === end($array))
        echo 'LAST ELEMENT!';
}

Do note there is also a warning on the post that this will only work if the array values are unique. If your last element is "world" and some random element in the middle is also "world" last element will execute twice.

请注意,帖子上还有一个警告,即只有在数组值是唯一的情况下这才有效。如果您的最后一个元素是“world”并且中间的一些随机元素也是“world”,那么最后一个元素将执行两次。

回答by dutt

first = true
foreach(...)
    if first
        do stuff
        first = false

回答by janoliver

hm

<?php
$i = 0;
foreach($ar as $sth) {
    if($i++ == 0) {
        // do something
    }
    // do something else
}

more elegant.

更优雅。

回答by omukiguy

This is also works

这也是作品

foreach($array as $element) {
    if ($element === reset($array))
        echo 'FIRST ELEMENT!';

    if ($element === end($array))
        echo 'LAST ELEMENT!';
}

回答by Mau Xanh Cua Linh

foreach($array as $element) {
    if ($element === reset($array))
        echo 'FIRST ELEMENT!';

    if ($element === end($array))
        echo 'LAST ELEMENT!';
}