php 如何测试数组指针是否在foreach循环中的第一个元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9176810/
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-26 06:18:44  来源:igfitidea点击:

how to test if array pointer is at first element in foreach loop

phpforeach

提问by AlexMorley-Finch

In a for loop it is simple...

在 for 循环中很简单......

for ( $idx = 0 ; $idx < count ( $array ) ; $idx ++ )
{
    if ( $idx == 0 )
    {
        // This is the first element of the array.
    }
}

How the hell is this done in a foreach loop?

这到底是如何在 foreach 循环中完成的?

is there a function like is_first()or something?

有类似的功能is_first()吗?

I'm looking for something like:

我正在寻找类似的东西:

foreach ( $array as $key => $value )
{
    if ( /* is the first element */ )
    {
        // do logic on first element
    }
    else
    {
        // all other logic
    }
}

I was thinking I could set a bool like $is_first = true;and then as soon as the loops been iterated once, set the bool to false.

我在想我可以设置一个 bool $is_first = true;,然后一旦循环迭代一次,就将 bool 设置为 false。

But php has a lot of pre-built functions and id rather use that... or another way...

但是 php 有很多预先构建的函数,而 id 宁愿使用它......或其他方式......

The whole bool way seem almost like... cheeting :s

整个布尔方式看起来几乎就像...... cheeting :s

Cheers,

干杯,

Alex

亚历克斯

回答by Choy

I usually do this :

我通常这样做:

$isFirst = true;
foreach($array as $key => $value){
  if($isFirst){
    //Do first stuff
  }else{
    //Do other stuff
  }
  $isFirst = false;
}

Works with any type of array, obviously.

显然,适用于任何类型的数组。

回答by RobinUS2

You can do this using "current()"

您可以使用“current()”来做到这一点

$myArray = array('a', 'b', 'c');
if (current($myArray) == $myArray[0]) {
    // We are at the first element
}

Docs: http://php.net/manual/en/function.current.php

文档:http: //php.net/manual/en/function.current.php

Ways of retrieving the first element:

检索第一个元素的方法:

$myArray[0]

$slice = array_slice($myArray, 0, 1); 
$elm = array_pop($slice);

回答by Mark Baker

$myArray = array('a' => 'first', 'b' => 'second', 'c' => 'third'); 

reset($myArray);
$firstKey = key($myArray);
foreach($myArray as $key => $value) {
    if ($key === $firstKey) {
        echo "I'm Spartacus" , PHP_EOL;
    }
    echo $key , " => " , $value , PHP_EOL;
}

回答by Haim Evgi

you can use counter instead bool

你可以使用 counter 代替 bool

$i = 0;

foreach ( $array as $key => $value )

    if ($i == 0) {
        // first
    } else  {
        // last
    }
    // …
    $i++;
}

or extract the first element by

或通过以下方式提取第一个元素

$first = array_shift($array);

and foreachover the remain array;

foreach在剩余数组上;

回答by Gaurav

$first = array_shift($idx);

foreach($idx as $key => $value){
...
...
...
}

回答by m0skit0

You can just put the operation on first element before the foreachloop, remove the element and then enter the foreachloop for the rest of the elements.

您可以将操作放在foreach循环之前的第一个元素上,删除该元素,然后为其余元素进入foreach循环。

回答by Dieter Gribnitz

Here are two functions that will detect if an array key is first or last.

这里有两个函数可以检测数组键是第一个还是最后一个。

If no key is provided it will assume the current pointer position.

如果没有提供键,它将采用当前的指针位置。

In a foreach loop you will need to provide the key since the pointer will not be correct.

在 foreach 循环中,您需要提供密钥,因为指针将不正确。

public static function isFirst($array, $key=null)
{
    if($key===null){
      $key = key($array);
    }
    reset($array);
    $first = key($array);
    return $first === $key;
}

public static function isLast($array, $key=null)
{
    if($key===null){
        $key = key($array);
    }
    end($array);
    $last = key($array);
    return $last === $key;
}