In Java, how do I get the index of the current element?
for (Element song: question){
song.currentIndex(); //<<want the current index
}
In PHP, you could do this:
foreach ($arr as $index => $value) {
echo "Key: $index; Value: $value";
}
You can’t, you either need to keep the index separately:
or use a normal for loop:
The reason is you can use the condensed for syntax to loop over any Iterable, and it’s not guaranteed that the values actually have an “index”