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

发布于 2025-01-03 05:26:05 字数 694 浏览 1 评论 0原文

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

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

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

有没有类似 is_first() 之类的函数?

我正在寻找类似的东西:

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

我想我可以设置一个像 $is_first = true; 这样的布尔值,然后一旦循环被迭代一次,就将布尔值设置为 false。

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

整个 bool 方式看起来几乎就像...... cheeting :

干杯,

Alex

In a for loop it is simple...

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?

is there a function like is_first() or something?

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.

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

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

Cheers,

Alex

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

国产ˉ祖宗 2025-01-10 05:26:06

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

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

在 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;
}

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.

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;
}
笛声青案梦长安 2025-01-10 05:26:05

我通常这样做:

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

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

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.

尐籹人 2025-01-10 05:26:05

您可以使用“current()”文档来执行此操作

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

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

获取第一个元素的方法:

$myArray[0]

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

You can do this using "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

Ways of retrieving the first element:

$myArray[0]

$slice = array_slice($myArray, 0, 1); 
$elm = array_pop($slice);
夜声 2025-01-10 05:26:05
$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;
}
$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;
}
朦胧时间 2025-01-10 05:26:05

您可以使用 counter 代替 bool

$i = 0;

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

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

第一个元素;

$first = array_shift($array);

或通过foreach 提取剩余数组中的

you can use counter instead 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 foreach over the remain array;

猫瑾少女 2025-01-10 05:26:05

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

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

我也只是我 2025-01-10 05:26:05
$first = array_shift($idx);

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

foreach($idx as $key => $value){
...
...
...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文