将数组的索引设置为变量
抱歉,但这可能是一个非常简单的答案。
我确实有一个数组:
Array ( [0] => 3 [1] => 0 )
如果我这样做:
foreach($array as $key){
$index = $key;
print_r($index);
}
当然我得到:
3
0
我想要一个索引为:
0
1 的
变量我该怎么做?应该很简单。我是在鄙视!感谢您的帮助!
Sorry, but it might be a very simple answer.
I do have an array:
Array ( [0] => 3 [1] => 0 )
If I do this:
foreach($array as $key){
$index = $key;
print_r($index);
}
Of course I get:
3
0
I want to have a variable with the index:
0
1
How do I do it? It should be very simple. I am disparing! Thanks for help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(3)
或者
or
...或使用 array_keys()
...or use array_keys()
foreach() 语句有两个版本,以下返回数组键和值。
$key
是数组 key (或 index),即。 0 和 1。$value
是相应数组$key
的 value,即。 3 和 0。foreach() 语句的另一种格式是您问题中的内容,仅返回数组 values (尽管您在代码中调用此
$key
), 所以...There are two versions of the foreach() statement, the following returns the array keys and values.
$key
is the array key (or index) ie. 0 and 1.$value
is the value for the corresponding array$key
ie. 3 and 0.The other format of the foreach() statement is what you have in your question and returns just the array values (although you call this
$key
in your code), so...