php 如何找到foreach索引?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/141108/
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
How to find the foreach index?
提问by user18334
Is it possible to find the foreachindex?
是否可以找到foreach索引?
in a forloop as follows:
在一个for循环如下:
for ($i = 0; $i < 10; ++$i) {
echo $i . ' ';
}
$iwill give you the index.
$i会给你索引。
Do I have to use the forloop or is there some way to get the index in the foreachloop?
我必须使用for循环还是有某种方法可以在foreach循环中获取索引?
回答by Owen
foreach($array as $key=>$value) {
// do stuff
}
$keyis the index of each $arrayelement
$key是每个$array元素的索引
回答by ConroyP
You can put a hack in your foreach, such as a field incremented on each run-through, which is exactly what the forloop gives you in a numerically-indexed array. Such a field would be a pseudo-index that needs manual management (increments, etc).
您可以在您的 中添加一个 hack foreach,例如在每次运行时递增的字段,这正是for循环在数字索引数组中为您提供的内容。这样的字段将是需要手动管理(增量等)的伪索引。
A foreachwill give you your index in the form of your $keyvalue, so such a hack shouldn't be necessary.
Aforeach将以您的$key价值的形式为您提供索引,因此不需要这样的黑客攻击。
e.g., in a foreach
例如,在一个 foreach
$index = 0;
foreach($data as $key=>$val) {
// Use $key as an index, or...
// ... manage the index this way..
echo "Index is $index\n";
$index++;
}
回答by Bailey Parker
回答by Zoredache
Owen has a good answer. If you want just the key, and you are working with an array this might also be useful.
欧文给出了很好的答案。如果您只想要密钥,并且您正在使用数组,这也可能很有用。
foreach(array_keys($array) as $key) {
// do stuff
}
回答by ólafur Waage
You can create $ioutside the loop and do $i++at the bottom of the loop.
您可以$i在循环外创建并$i++在循环底部执行。
回答by Trev
These two loops are equivalent (bar the safety railings of course):
这两个循环是等效的(当然不包括安全栏杆):
for ($i=0; $i<count($things); $i++) { ... }
foreach ($things as $i=>$thing) { ... }
eg
例如
for ($i=0; $i<count($things); $i++) {
echo "Thing ".$i." is ".$things[$i];
}
foreach ($things as $i=>$thing) {
echo "Thing ".$i." is ".$thing;
}
回答by Mikel Williams
I think best option is like same:
我认为最好的选择是一样的:
foreach ($lists as $key=>$value) {
echo $key+1;
}
it is easy and normally
这很容易而且通常
回答by The Brawny Man
Jonathan is correct. PHP arrays act as a map table mapping keys to values. in some cases you can get an index if your array is defined, such as
乔纳森是对的。PHP 数组充当映射表,将键映射到值。在某些情况下,如果定义了数组,则可以获得索引,例如
$var = array(2,5);
for ($i = 0; $i < count($var); $i++) {
echo $var[$i]."\n";
}
your output will be
你的输出将是
2
5
in which case each element in the array has a knowable index, but if you then do something like the following
在这种情况下,数组中的每个元素都有一个已知的索引,但是如果您执行以下操作
$var = array_push($var,10);
for ($i = 0; $i < count($var); $i++) {
echo $var[$i]."\n";
}
you get no output. This happens because arrays in PHP are not linear structures like they are in most languages. They are more like hash tables that may or may not have keys for all stored values. Hence foreach doesn't use indexes to crawl over them because they only have an index if the array is defined. If you need to have an index, make sure your arrays are fully defined before crawling over them, and use a for loop.
你没有输出。发生这种情况是因为 PHP 中的数组不是像大多数语言中那样的线性结构。它们更像是哈希表,可能有也可能没有所有存储值的键。因此 foreach 不使用索引来抓取它们,因为如果定义了数组,它们只有一个索引。如果需要索引,请确保在遍历数组之前已完全定义数组,并使用 for 循环。
回答by The Brawny Man
PHP arrays have internal pointers, so try this:
PHP 数组有内部指针,所以试试这个:
foreach($array as $key => $value){
$index = current($array);
}
Works okay for me (only very preliminarily tested though).
对我来说没问题(虽然只是非常初步测试)。
回答by Randy Greencorn
I normally do this when working with associative arrays:
我通常在使用关联数组时这样做:
foreach ($assoc_array as $key => $value) {
//do something
}
This will work fine with non-associative arrays too. $key will be the index value. If you prefer, you can do this too:
这也适用于非关联数组。$key 将是索引值。如果您愿意,也可以这样做:
foreach ($array as $indx => $value) {
//do something
}

