How to start a foreach loop at a specific index in PHP

前端 未结 6 2019
野的像风
野的像风 2020-12-08 08:59

I am writing a foreach that does not start at the 0th index but instead starts at the first index of my array. Is there any way to offset the loop\'s starting p

6条回答
  •  长情又很酷
    2020-12-08 09:47

    You can use the array_slice function:

    $arr = array(); // your array
    foreach(array_slice($arr, 1) as $foo){
       // do what ever you want here
    }
    

    Of course, you can use whatever offset value you want. In this case, 1 'skip' the first element of the array.

提交回复
热议问题