How can I get the current array index in a foreach loop?

前端 未结 10 1733
陌清茗
陌清茗 2020-12-14 06:19

How do I get the current index in a foreach loop?

foreach ($arr as $key => $val)
{
    // How do I get the index?
    // How do I get the fir         


        
10条回答
  •  执念已碎
    2020-12-14 06:37

    The current index is the value of $key. And for the other question, you can also use:

    current($arr)
    

    to get the first element of any array, assuming that you aren't using the next(), prev() or other functions to change the internal pointer of the array.

提交回复
热议问题