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 … ????
$ais a variable,($a)is an expression resulting in the value of$a.keyexpects a variable passed by reference. Not sure what exactlykeydoes with that and why it behaves the way it behaves, but I'd file this under wrong, undefined behavior.