如何遍历关联数组并获取键?

时间:2021-11-25 21:30:14

My associative array:

我的关联数组:

$arr = array(
   1 => "Value1",
   2 => "Value2",
   10 => "Value10"
);

Using the following code, $v is filled with $arr's values

使用以下代码,$v填充$arr的值

 foreach($arr as $v){
    echo($v);    // Value1, Value2, Value10
 }

How do I get $arr's keys instead?

我怎么才能得到$arr的钥匙呢?

 foreach(.....){
    echo($k);    // 1, 2, 10
 }

11 个解决方案

#1


247  

You can do:

你能做什么:

foreach ($arr as $key => $value) {
 echo $key;
}

As described in PHP docs.

如PHP文档中所述。

#2


57  

If you use array_keys(), PHP will give you an array filled with just the keys:

如果您使用array_keys(), PHP将为您提供一个仅包含键的数组:

$keys = array_keys($arr);
foreach($keys as $key) {
    echo($key);
}

Alternatively, you can do this:

或者,你也可以这样做:

foreach($arr as $key => $value) {
    echo($key);
}

#3


21  

Nobody answered with regular for loop? Sometimes I find it more readable and prefer for over foreach
So here it is:

没有人用正则循环来回答?有时我觉得它更容易读,更喜欢foreach所以这里是:

$array = array('key1' => 'value1', 'key2' => 'value2'); 

$keys = array_keys($array);

for($i=0; $i < count($keys); ++$i) {
    echo $keys[$i] . ' ' . $array[$keys[$i]] . "\n";
}

/*
  prints:
  key1 value1
  key2 value2
*/

#4


10  

foreach($array as $k => $v)

Where $k is the key and $v is the value

k是关键,v是价值

Or if you just need the keys use array_keys()

或者如果你只需要键使用array_keys()

#5


7  

I use the following loop to get the key and value from an associative array

我使用下面的循环来从关联数组中获取键和值。

foreach ($array as $key => $value)
{
  echo "<p>$key = $value</p>";
}

#6


4  

The following will allow you to get at both the key and value at the same time.

下面将允许您同时获取键和值。

foreach ($arr as $key => $value)
{
  echo($key);
}

#7


3  

While arguably being less clear this method is faster by roughly a factor of roughly 3.5 (At least on the box I used to test)

虽然可能不太清楚,但这种方法的速度大约是3.5倍(至少在我用来测试的盒子上)

$foo = array(
    1 => "Value1",
    2 => "Value2",
    10 => "Value10"
);
while($bar = each($foo)){
    echo $bar[0] . " => " . $bar[1];
}

I would imagine that this is due to the fact the foreach copies the entire array before iterating over it.

我认为这是由于foreach在迭代之前复制了整个数组。

#8


1  

Use $key => $val to get the keys:

使用$key => $val获取密钥:

<?php

$arr = array(
    1 => "Value1",
    2 => "Value2",
    10 => "Value10",
);

foreach ($arr as $key => $val) {
   print "$key\n";
}

?>

#9


1  

<?php
$names = array("firstname"=>"maurice",
               "lastname"=>"muteti", 
               "contact"=>"7844433339");

foreach ($names as $name => $value) {
    echo $name." ".$value."</br>";
}

print_r($names);
?>

#10


1  

Oh I found it in the PHP manual.

我在PHP手册里找到的。

foreach ($array as $key => $value){
    statement
}

The current element's key will be assigned to the variable $key on each loop.

当前元素的键将被分配给每个循环上的变量$key。

#11


0  

 foreach($arr as $key=>$value){
    echo($key);    // key
 }

#1


247  

You can do:

你能做什么:

foreach ($arr as $key => $value) {
 echo $key;
}

As described in PHP docs.

如PHP文档中所述。

#2


57  

If you use array_keys(), PHP will give you an array filled with just the keys:

如果您使用array_keys(), PHP将为您提供一个仅包含键的数组:

$keys = array_keys($arr);
foreach($keys as $key) {
    echo($key);
}

Alternatively, you can do this:

或者,你也可以这样做:

foreach($arr as $key => $value) {
    echo($key);
}

#3


21  

Nobody answered with regular for loop? Sometimes I find it more readable and prefer for over foreach
So here it is:

没有人用正则循环来回答?有时我觉得它更容易读,更喜欢foreach所以这里是:

$array = array('key1' => 'value1', 'key2' => 'value2'); 

$keys = array_keys($array);

for($i=0; $i < count($keys); ++$i) {
    echo $keys[$i] . ' ' . $array[$keys[$i]] . "\n";
}

/*
  prints:
  key1 value1
  key2 value2
*/

#4


10  

foreach($array as $k => $v)

Where $k is the key and $v is the value

k是关键,v是价值

Or if you just need the keys use array_keys()

或者如果你只需要键使用array_keys()

#5


7  

I use the following loop to get the key and value from an associative array

我使用下面的循环来从关联数组中获取键和值。

foreach ($array as $key => $value)
{
  echo "<p>$key = $value</p>";
}

#6


4  

The following will allow you to get at both the key and value at the same time.

下面将允许您同时获取键和值。

foreach ($arr as $key => $value)
{
  echo($key);
}

#7


3  

While arguably being less clear this method is faster by roughly a factor of roughly 3.5 (At least on the box I used to test)

虽然可能不太清楚,但这种方法的速度大约是3.5倍(至少在我用来测试的盒子上)

$foo = array(
    1 => "Value1",
    2 => "Value2",
    10 => "Value10"
);
while($bar = each($foo)){
    echo $bar[0] . " => " . $bar[1];
}

I would imagine that this is due to the fact the foreach copies the entire array before iterating over it.

我认为这是由于foreach在迭代之前复制了整个数组。

#8


1  

Use $key => $val to get the keys:

使用$key => $val获取密钥:

<?php

$arr = array(
    1 => "Value1",
    2 => "Value2",
    10 => "Value10",
);

foreach ($arr as $key => $val) {
   print "$key\n";
}

?>

#9


1  

<?php
$names = array("firstname"=>"maurice",
               "lastname"=>"muteti", 
               "contact"=>"7844433339");

foreach ($names as $name => $value) {
    echo $name." ".$value."</br>";
}

print_r($names);
?>

#10


1  

Oh I found it in the PHP manual.

我在PHP手册里找到的。

foreach ($array as $key => $value){
    statement
}

The current element's key will be assigned to the variable $key on each loop.

当前元素的键将被分配给每个循环上的变量$key。

#11


0  

 foreach($arr as $key=>$value){
    echo($key);    // key
 }