How to loop through an associative array and get the key?

PhpLoopsAssociative Array

Php Problem Overview


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
 }

Php Solutions


Solution 1 - Php

You can do:

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

As described in PHP docs.

Solution 2 - Php

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);
}

Solution 3 - Php

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
*/

Solution 4 - Php

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()

Solution 5 - Php

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>";
}

Solution 6 - Php

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

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

Solution 7 - Php

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.

Solution 8 - Php

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.

Solution 9 - Php

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

<?php

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

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

?>

Solution 10 - Php

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

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

print_r($names);
?>

Solution 11 - Php

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

Solution 12 - Php

If you use nested foreach() function, outer array's keys print again and again till inner array values end.

<?php 

$myArray = ['key_1' => ['value_1', 'value12'],
            'key_2' => ['value_2', 'value22'], 
            'key_3' => ['value_3', 'value32']
           ];

$keysOfMyArray = array_key($myArray);

for ($x = 0; $x < count($myArray); $x++){
       print "\t".$keysOfMyArray[$x]."\t\t".implode("\t\t",$myArray[$keysOfMyArray[$x]]."\n");
}

?>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRobin RodricksView Question on Stackoverflow
Solution 1 - PhpcodaddictView Answer on Stackoverflow
Solution 2 - PhpTrevor JohnsView Answer on Stackoverflow
Solution 3 - PhpMuhsinFatihView Answer on Stackoverflow
Solution 4 - PhpHtbaaView Answer on Stackoverflow
Solution 5 - PhpdmeehanView Answer on Stackoverflow
Solution 6 - PhpJeff BeckView Answer on Stackoverflow
Solution 7 - PhpnettuxView Answer on Stackoverflow
Solution 8 - PhpRobin RodricksView Answer on Stackoverflow
Solution 9 - PhpraphinkView Answer on Stackoverflow
Solution 10 - PhpmauriceView Answer on Stackoverflow
Solution 11 - PhpGravitonView Answer on Stackoverflow
Solution 12 - PhpHewarathna Ashen IrangaView Answer on Stackoverflow