7

look at this:

<?php
    $array = array('a' => '…', 'b' => '…', 'c' => '…', 'd' => '…', 'e' => '…', 'f' => '…');
    foreach ($array as $key => $val){
        echo "current key: $key, next key: ".key(($array))."<br>";
    }
?>

OUTPUT:

current key: a, next key: b
current key: b, next key: c
current key: c, next key: d
current key: d, next key: e
current key: e, next key: f
current key: f, next key: a

I was searching for a function to get the next key of an associative array within a foreach-loop. i tried a bit and suddenly it worked. (as you can see in my example).

BUT WHY DOES THIS WORK? Does it make sense? … not to me! Can you explain this to me?

It's because of the key(($array)) part but why? i mean.. it was a mistake.. i wanted to write key($array) but I forgot to delete the 2 wrapping brackets. So it was coincidence !!!

Why does it behave this way? i mean, it's good but … ????

5
  • 3
    Interesting. If you switch on error reporting, you'll see lots of errors. The extra brackets are breaking the direct variable reference. $a is a variable, ($a) is an expression resulting in the value of $a. key expects a variable passed by reference. Not sure what exactly key does with that and why it behaves the way it behaves, but I'd file this under wrong, undefined behavior. Commented Dec 2, 2012 at 19:33
  • 1
    whoa, if this is intended behavior awesome. There have been so many times I could have used something like this. Commented Dec 2, 2012 at 19:34
  • but what do you suggest? shall i keep it that way? Commented Dec 2, 2012 at 19:36
  • 4
    Definitely no. The behavior is not even consistent across PHP versions, you cannot rely on this: 3v4l.org/5RjfA Commented Dec 2, 2012 at 19:38
  • ok. that's a good one. thx. Commented Dec 2, 2012 at 19:42

1 Answer 1

4

According to the PHP Manual for key, key() returns the index element of the current array position.

The problem isn't so much with key, or even with the double parentheses. Key receives the array by reference, so the double parentheses aren't doing much.

The behavior comes from foreach. When foreach iterates through the array, different versions of PHP will behave different on setting the array's internal current pointer, which is what key(), next(), current(), etc, are looking at when they are called.

Arrays in PHP aren't like arrays in most languages; they are really objects (especially associative arrays). Think of them kinda like linked lists (they are not linked lists, but just go with me for illustration purposes) - when you iterate through, you need to know where you are currently at and where you are going to be next.

What is apparently happening here is that on whatever version of PHP you are running, foreach is setting the internal current pointer to the next element at the beginning of the for loop, immediately after setting the $key and $value variables in your code.

I would definitely not depend on this behavior, as subsequent updates to PHP may break this code. It's just a fun coincidence of this specific version. If you want the next key, look at replacing your foreach loop. The PHP manual on next() has good examples, and be sure to also check out prev(), each(), and the other functions in the "see also" section.

Sign up to request clarification or add additional context in comments.

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.