208

My associative array:

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

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

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

How do I get $arr's keys instead?

 foreach (.....){
    echo $k;    // 1, 2, 10
 }
Dharman
  • 30,962
  • 25
  • 85
  • 135
Robin Rodricks
  • 110,798
  • 141
  • 398
  • 607

11 Answers11

394

You can do:

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

As described in PHP docs.

codaddict
  • 445,704
  • 82
  • 492
  • 529
82

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

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

Alternatively, you can do this:

foreach ($arr as $key => $value) {
    echo $key;
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Trevor Johns
  • 15,682
  • 3
  • 55
  • 54
43

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

$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
*/
MuhsinFatih
  • 1,891
  • 2
  • 24
  • 31
  • 4
    This is useful in some circumstances when foreach glitches out for unexplainable reasons. Good to always have at least two ways to do things. – lilHar Apr 19 '17 at 20:45
  • 15
    There is no "unexplainable" reasons when it comes to computers. – jdrake Nov 21 '17 at 10:02
  • 1
    Also useful for when you want to combine two subsequent array items together. i+=2 and then using $array[$keys[$i]]."_".$array[$keys[$i+1]] Just incase someone else has this same problem – Keeghan McGarry Jan 12 '18 at 10:49
  • 2
    The bugs in foreach are described here: https://www.php.net/manual/en/control-structures.foreach.php If you are using PHP 7, nested foreaches and foreach references work as intended. If you are using PHP 5, you should avoid using foreach by reference values and since all foreaches use internal array pointer ( current($Array) ) nesting foreaches or using PHP array functions can do weird things. – Chaoix Jul 12 '19 at 15:13
  • @jdrake That would be so funny, if randomly foreach does not work and you just have to relace by a for-loop. This is something they should have implemented in INTERCAL – Adam Apr 25 '22 at 12:20
11
foreach($array as $k => $v)

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

Or if you just need the keys use array_keys()

Htbaa
  • 2,319
  • 18
  • 28
8

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>";
}
dmeehan
  • 2,317
  • 2
  • 26
  • 31
  • 2
    The only answer that describes clearly and concisely how to use both key and value in the loop! – SharpC Sep 21 '17 at 19:42
5

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

foreach ($arr as $key => $value)
{
  echo($key);
}
Jeff Beck
  • 3,944
  • 3
  • 28
  • 45
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)

$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.

nettux
  • 5,270
  • 2
  • 23
  • 33
2
 foreach($arr as $key=>$value){
    echo($key);    // key
 }
Graviton
  • 81,782
  • 146
  • 424
  • 602
  • 1
    Love that this was posted 4 seconds after codeaddict's answer, and 13 years later it's got a 0 score vs 381 score. Have a pity upvote lol – miken32 Nov 05 '22 at 17:19
1

Oh I found it in the PHP manual.

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

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

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Robin Rodricks
  • 110,798
  • 141
  • 398
  • 607
1

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

<?php

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

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

?>
raphink
  • 3,625
  • 1
  • 28
  • 39
0
<?php
$names = array("firstname"=>"maurice",
               "lastname"=>"muteti", 
               "contact"=>"7844433339");

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

print_r($names);
?>
maurice
  • 5
  • 5