如何在foreach循环中获取当前数组索引?

时间:2021-12-29 01:26:16

How do I get the current index in a foreach loop?

如何在foreach循环中获取当前索引?

foreach ($arr as $key => $val)
{
    // How do I get the index?
    // How do I get the first element in an associative array?
}

9 个解决方案

#1


49  

In your sample code, it would just be $key.

在您的示例代码中,它只是$ key。

If you want to know, for example, if this is the first, second, or ith iteration of the loop, this is your only option:

如果你想知道,例如,如果这是循环的第一次,第二次或第i次迭代,这是你唯一的选择:

$i = -1;
foreach($arr as $val) {
  $i++;
  //$i is now the index.  if $i == 0, then this is the first element.
  ...
}

Of course, this doesn't mean that $val == $arr[$i] because the array could be an associative array.

当然,这并不意味着$ val == $ arr [$ i]因为数组可能是一个关联数组。

#2


15  

This is the most exhaustive answer so far and gets rid of the need for a $i variable floating around. It is a combo of Kip and Gnarf's answers.

到目前为止,这是最详尽的答案,并且不需要浮动$ i变量。它是Kip和Gnarf答案的组合。

$array = array( 'cat' => 'meow', 'dog' => 'woof', 'cow' => 'moo', 'computer' => 'beep' );
foreach( array_keys( $array ) as $index=>$key ) {

    // display the current index + key + value
    echo $index . ':' . $key . $array[$key];

    // first index
    if ( $index == 0 ) {
        echo ' -- This is the first element in the associative array';
    }

    // last index
    if ( $index == count( $array ) - 1 ) {
        echo ' -- This is the last element in the associative array';
    }
    echo '<br>';
}

Hope it helps someone.

希望它可以帮到某人。

#3


11  

$i = 0;
foreach ($arr as $key => $val) {
  if ($i === 0) {
    // first index
  }
  // current index is $i

  $i++;
}

#4


7  

foreach($array as $key=>$value) {
    // do stuff
}

$key is the index of each $array element

$ key是每个$数组元素的索引

#5


4  

You can get the index value with this

您可以使用此获取索引值

foreach ($arr as $key => $val)
{
    $key = (int) $key;
    //With the variable $key you can get access to the current array index
    //You can use $val[$key] to

}

#6


3  

The current index is the value of $key. And for the other question, you can also use:

当前索引是$ key的值。对于另一个问题,您还可以使用:

current($arr)

to get the first element of any array, assuming that you aren't using the next(), prev() or other functions to change the internal pointer of the array.

获取任何数组的第一个元素,假设您没有使用next(),prev()或其他函数来更改数组的内部指针。

#7


0  

$key is the index for the current array element, and $val is the value of that array element.

$ key是当前数组元素的索引,$ val是该数组元素的值。

The first element has an index of 0. Therefore, to access it, use $arr[0]

第一个元素的索引为0.因此,要访问它,请使用$ arr [0]

To get the first element of the array, use this

要获取数组的第一个元素,请使用此方法

$firstFound = false;
foreach($arr as $key=>$val)
{
    if (!$firstFound)
       $first = $val;
    else
       $firstFound = true;
    // do whatever you want here
}

// now ($first) has the value of the first element in the array

#8


0  

You could get the first element in the array_keys() function as well. Or array_search() the keys for the "index" of a key. If you are inside a foreach loop, the simple incrementing counter (suggested by kip or cletus) is probably your most efficient method though.

您也可以获得array_keys()函数中的第一个元素。或者array_search()键的“索引”的键。如果你在foreach循环中,简单的递增计数器(由kip或cletus建议)可能是你最有效的方法。

<?php
   $array = array('test', '1', '2');
   $keys = array_keys($array);
   var_dump($keys[0]); // int(0)

   $array = array('test'=>'something', 'test2'=>'something else');
   $keys = array_keys($array);

   var_dump(array_search("test2", $keys)); // int(1)     
   var_dump(array_search("test3", $keys)); // bool(false)

#9


0  

well since this is the first google hit for this problem:

好吧,因为这是第一个谷歌打这个问题:

function mb_tell(&$msg) {
    if(count($msg) == 0) {
        return 0;
    }
    //prev($msg);
    $kv = each($msg);
    if(!prev($msg)) {
        end($msg);

        print_r($kv);
        return ($kv[0]+1);
    }
    print_r($kv);
    return ($kv[0]);
}

#1


49  

In your sample code, it would just be $key.

在您的示例代码中,它只是$ key。

If you want to know, for example, if this is the first, second, or ith iteration of the loop, this is your only option:

如果你想知道,例如,如果这是循环的第一次,第二次或第i次迭代,这是你唯一的选择:

$i = -1;
foreach($arr as $val) {
  $i++;
  //$i is now the index.  if $i == 0, then this is the first element.
  ...
}

Of course, this doesn't mean that $val == $arr[$i] because the array could be an associative array.

当然,这并不意味着$ val == $ arr [$ i]因为数组可能是一个关联数组。

#2


15  

This is the most exhaustive answer so far and gets rid of the need for a $i variable floating around. It is a combo of Kip and Gnarf's answers.

到目前为止,这是最详尽的答案,并且不需要浮动$ i变量。它是Kip和Gnarf答案的组合。

$array = array( 'cat' => 'meow', 'dog' => 'woof', 'cow' => 'moo', 'computer' => 'beep' );
foreach( array_keys( $array ) as $index=>$key ) {

    // display the current index + key + value
    echo $index . ':' . $key . $array[$key];

    // first index
    if ( $index == 0 ) {
        echo ' -- This is the first element in the associative array';
    }

    // last index
    if ( $index == count( $array ) - 1 ) {
        echo ' -- This is the last element in the associative array';
    }
    echo '<br>';
}

Hope it helps someone.

希望它可以帮到某人。

#3


11  

$i = 0;
foreach ($arr as $key => $val) {
  if ($i === 0) {
    // first index
  }
  // current index is $i

  $i++;
}

#4


7  

foreach($array as $key=>$value) {
    // do stuff
}

$key is the index of each $array element

$ key是每个$数组元素的索引

#5


4  

You can get the index value with this

您可以使用此获取索引值

foreach ($arr as $key => $val)
{
    $key = (int) $key;
    //With the variable $key you can get access to the current array index
    //You can use $val[$key] to

}

#6


3  

The current index is the value of $key. And for the other question, you can also use:

当前索引是$ key的值。对于另一个问题,您还可以使用:

current($arr)

to get the first element of any array, assuming that you aren't using the next(), prev() or other functions to change the internal pointer of the array.

获取任何数组的第一个元素,假设您没有使用next(),prev()或其他函数来更改数组的内部指针。

#7


0  

$key is the index for the current array element, and $val is the value of that array element.

$ key是当前数组元素的索引,$ val是该数组元素的值。

The first element has an index of 0. Therefore, to access it, use $arr[0]

第一个元素的索引为0.因此,要访问它,请使用$ arr [0]

To get the first element of the array, use this

要获取数组的第一个元素,请使用此方法

$firstFound = false;
foreach($arr as $key=>$val)
{
    if (!$firstFound)
       $first = $val;
    else
       $firstFound = true;
    // do whatever you want here
}

// now ($first) has the value of the first element in the array

#8


0  

You could get the first element in the array_keys() function as well. Or array_search() the keys for the "index" of a key. If you are inside a foreach loop, the simple incrementing counter (suggested by kip or cletus) is probably your most efficient method though.

您也可以获得array_keys()函数中的第一个元素。或者array_search()键的“索引”的键。如果你在foreach循环中,简单的递增计数器(由kip或cletus建议)可能是你最有效的方法。

<?php
   $array = array('test', '1', '2');
   $keys = array_keys($array);
   var_dump($keys[0]); // int(0)

   $array = array('test'=>'something', 'test2'=>'something else');
   $keys = array_keys($array);

   var_dump(array_search("test2", $keys)); // int(1)     
   var_dump(array_search("test3", $keys)); // bool(false)

#9


0  

well since this is the first google hit for this problem:

好吧,因为这是第一个谷歌打这个问题:

function mb_tell(&$msg) {
    if(count($msg) == 0) {
        return 0;
    }
    //prev($msg);
    $kv = each($msg);
    if(!prev($msg)) {
        end($msg);

        print_r($kv);
        return ($kv[0]+1);
    }
    print_r($kv);
    return ($kv[0]);
}