Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am sure that this is super easy and built-in function in PHP, but I have yet not seen it.

Here's what I am doing for the moment:

foreach($array as $key => $value) {
    echo $key; // Would output "subkey" in the example array
    print_r($value);
}

Could I do something like the following instead and thereby save myself from writing "$key => $value" in every foreach loop? (psuedocode)

foreach($array as $subarray) {
    echo arrayKey($subarray); // Will output the same as "echo $key" in the former example ("subkey"
    print_r($value);
}

Thanks!

The array:

Array
(
    [subKey] => Array
        (
            [value] => myvalue
        )

)
share|improve this question
7  
Whats wrong with foreach($array as $key => $value)? Or, asked the other way around, what's the point using foreach($array as $value) when you actually need $key somewhere down the road? –  Tomalak Jul 23 '10 at 12:10
2  
I just thought it could be a good idea to get the key in a quick way –  Industrial Jul 23 '10 at 12:12
1  
But you do get it in a quick way with foreach($array as $key => $value)... :-) Or is the situation not that you are in a foreach loop? –  Tomalak Jul 23 '10 at 12:14
    
I am in a foreach loop for sure, but what i've thought about was to not change the foreach statement, but just printing out the key. –  Industrial Jul 23 '10 at 12:21
2  
Every function call you could make would be less efficient than simply changing to the appropriate foreach loop construct. –  Tomalak Jul 23 '10 at 13:48

7 Answers 7

up vote 34 down vote accepted

You can use key():

<?php
$array = array(
    "one" => 1,
    "two" => 2,
    "three" => 3,
    "four" => 4
);

while($element = current($array)) {
    echo key($array)."\n";
    next($array);
}
?>
share|improve this answer
4  
Hi! But key doesnt work in a foreach, right? –  Industrial Jul 23 '10 at 12:49
1  
Yes, that is correct. You need to use while-loop for key(). –  vtorhonen Jul 23 '10 at 12:59
2  
I want to add that you can use key() ANYWHERE but for this problem it make sense to use it in a while-loop. You can use key() in the instance of only wanting the first/current array element's key. –  J.Romero Nov 21 '12 at 19:50
2  
@Industrial foreach uses the key... if you want to use foreach, do this: foreach($array as $key => $value) { ... } –  Greg Oct 16 '13 at 15:27

Dogbert's solution was my preferred answer to this, thanks.

        foreach ($array as $key => $value) {
            echo "$key: $value <br />";
        }
share|improve this answer

Another way to use key($array) in a foreach loop is by using next($array) at the end of the loop, just make sure each iteration calls the next() function (in case you have complex branching inside the loop)

share|improve this answer
$foo = array('a' => 'apple', 'b' => 'ball', 'c' => 'coke');

foreach($foo as $key => $item) {
  echo $item.' is begin with ('.$key.')';
}
share|improve this answer

If it IS a foreach loop as you have described in the question, using $key => $value is fast and efficient.

share|improve this answer

$array = array(0 => 100, "color" => "red");
print_r(array_keys($array));

share|improve this answer
    
what's d difference? –  gumuruh Apr 18 '12 at 9:17

Use the array_search function.

Example from php.net

$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');

$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array);   // $key = 1;
share|improve this answer
    
Hi, updated my original post with an array example. Thanks! –  Industrial Jul 23 '10 at 12:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.