32

How can I get the current element number when I'm traversing a array?

I know about count(), but I was hoping there's a built-in function for getting the current field index too, without having to add a extra counter variable.

like this:

foreach($array as $key => value)
  if(index($key) == count($array) ....
5
  • What's wrong with using $key? Commented Sep 21, 2010 at 21:32
  • $key is a string in my case. anyway I was just wondering if there's a php function that can get the field index. if not, I'll just use a $i counter... Commented Sep 21, 2010 at 21:33
  • In the case of an array where the key does not match the index of the array element, I believe Alex would like to obtain the current index. E.g.: a => apples, b => bananas: then index(b) would return 1. Commented Sep 21, 2010 at 21:34
  • field index == key. it's the same things. And no, there is no way to get item's position. Commented Sep 21, 2010 at 21:40
  • Foreach works with an iterator - the index is never known Commented Sep 21, 2010 at 21:42

6 Answers 6

61

You should use the key() function.

key($array)

should return the current key.

If you need the position of the current key:

array_search($key, array_keys($array));
Sign up to request clarification or add additional context in comments.

4 Comments

the OP wants a position, not a key. No need to get a kay as it;a already assigned to $key variable
+1 Good answer, I would just like to point out to Alex that adding a counter would be more efficient that searching the array each time though.
so there's no index-like function. thanks :) I'll just use a counter
I'd dovnvote it as it's seems way overkill to me. 2 additional loops over whole array per iteration!
13

PHP arrays are both integer-indexed and string-indexed. You can even mix them:

array('red', 'green', 'white', 'color3'=>'blue', 3=>'yellow');

What do you want the index to be for the value 'blue'? Is it 3? But that's actually the index of the value 'yellow', so that would be an ambiguity.

Another solution for you is to coerce the array to an integer-indexed list of values.

foreach (array_values($array) as $i => $value) {
  echo "$i: $value\n";
}

Output:

0: red
1: green
2: white
3: blue
4: yellow

Comments

4
foreach() {
    $i++;
    if(index($key) == $i){}
    //
}

1 Comment

he said without having to add a extra counter variable
0
function Index($index) {
    $Count = count($YOUR_ARRAY);
    if ($index <= $Count) {
        $Keys = array_keys($YOUR_ARRAY);
        $Value = array_values($YOUR_ARRAY);
        return $Keys[$index] . ' = ' . $Value[$index];
    } else {
        return "Out of the ring";
    }
}

echo 'Index : ' . Index(0);

Replace the ( $YOUR_ARRAY )

Comments

-1

I recently had to figure this out for myself and ended up on a solution inspired by @Zahymaka 's answer, but solving the 2x looping of the array.

What you can do is create an array with all your keys, in the order they exist, and then loop through that.

        $keys=array_keys($items);
        foreach($keys as $index=>$key){
                    echo "position: $index".PHP_EOL."item: ".PHP_EOL;
                    var_dump($items[$key]);
                    ...
        }

PS: I know this is very late to the party, but since I found myself searching for this, maybe this could be helpful to someone else

Comments

-2

There is no way to get a position which you really want.
For associative array, to determine last iteration you can use already mentioned counter variable, or determine last item's key first:

end($array);
$last = key($array);
foreach($array as $key => value)
  if($key == $last) ....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.