PHP - 从数组中获取元素的索引

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

PHP - Getting the index of a element from a array

phparraysfield

提问by Alex

How can I get the current element number when I'm traversing a array?

遍历数组时如何获取当前元素编号?

I know about count(), but I was hoping there's a built-in function for getting the current field index too, without having to add a extra counter variable.

我知道count(),但我希望有一个内置函数来获取当前字段索引,而不必添加额外的计数器变量。

like this:

像这样:

foreach($array as $key => value)
  if(index($key) == count($array) ....

回答by Zahymaka

You should use the key()function.

您应该使用key()函数。

key($array)

should return the current key.

应该返回当前密钥。

If you need the position of the current key:

如果需要当前键的位置:

array_search($key, array_keys($array));

回答by Bill Karwin

PHP arrays are both integer-indexed and string-indexed. You can even mix them:

PHP 数组既是整数索引的,也是字符串索引的。你甚至可以混合它们:

array('red', 'green', 'white', 'color3'=>'blue', 3=>'yellow');

What do you want the index to be for the value 'blue'? Is it 3? But that's actually the index of the value 'yellow', so that would be an ambiguity.

您希望索引的值是'blue'什么?是3吗?但这实际上是 value 的索引'yellow',所以这将是模棱两可的。

Another solution for you is to coerce the array to an integer-indexed list of values.

另一个解决方案是将数组强制为整数索引的值列表。

foreach (array_values($array) as $i => $value) {
  echo "$i: $value\n";
}

Output:

输出:

0: red
1: green
2: white
3: blue
4: yellow

回答by Galen

foreach() {
    $i++;
    if(index($key) == $i){}
    //
}

回答by Сайф Абаза

function Index($index) {
    $Count = count($YOUR_ARRAY);
    if ($index <= $Count) {
        $Keys = array_keys($YOUR_ARRAY);
        $Value = array_values($YOUR_ARRAY);
        return $Keys[$index] . ' = ' . $Value[$index];
    } else {
        return "Out of the ring";
    }
}

echo 'Index : ' . Index(0);

Replace the ( $YOUR_ARRAY )

替换( $YOUR_ARRAY )

回答by Ant

I recently had to figure this out for myself and ended up on a solution inspired by @Zahymaka 's answer, but solving the 2x looping of the array.

我最近不得不自己解决这个问题,最终得到了一个受@Zahymaka 答案启发的解决方案,但解决了数组的 2x 循环。

What you can do is create an array with all your keys, in the order they exist, and then loop through that.

你可以做的是用你所有的键创建一个数组,按照它们存在的顺序,然后循环遍历。

        $keys=array_keys($items);
        foreach($keys as $index=>$key){
                    echo "position: $index".PHP_EOL."item: ".PHP_EOL;
                    var_dump($items[$key]);
                    ...
        }

PS: I know this is very late to the party, but since I found myself searching for this, maybe this could be helpful to someone else

PS:我知道这对派对来说已经很晚了,但是因为我发现自己正在寻找这个,也许这对其他人有帮助

回答by Your Common Sense

There is no way to get a positionwhich you really want.
For associative array, to determine last iteration you can use already mentioned counter variable, or determine last item's key first:

没有办法获得你真正想要的职位
对于关联数组,要确定最后一次迭代,您可以使用已经提到的计数器变量,或者首先确定最后一项的键:

end($array);
$last = key($array);
foreach($array as $key => value)
  if($key == $last) ....

回答by thomasmalt

an array does not contain index when elements are associative. An array in php can contain mixed values like this:

当元素关联时,数组不包含索引。php 中的数组可以包含如下混合值:

$var = array("apple", "banana", "foo" => "grape", "carrot", "bar" => "donkey");   
print_r($var);

Gives you:

给你:

Array
(
    [0] => apple
    [1] => banana
    [foo] => grape
    [2] => carrot
    [bar] => donkey
)

What are you trying to achieve since you need the index value in an associative array?

由于您需要关联数组中的索引值,您想要实现什么?