PHP - Getting the index of a element from a array

后端 未结 7 2100
既然无缘
既然无缘 2021-02-02 08:40

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

相关标签:
7条回答
  • 2021-02-02 09:43

    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) ....
    
    0 讨论(0)
提交回复
热议问题