How to access N-th element of an array in PHP

后端 未结 3 904
误落风尘
误落风尘 2021-02-07 11:36

I\'m embarrassed to ask this and it\'s most likely a duplicate, but my google results are coming up short (im searching incorrectly I guess) and such a basic question is infuria

3条回答
  •  执笔经年
    2021-02-07 12:35

    If you want to access the nth element without knowing the index, you can use next() n times to reach the nth element.

    for($i = 0; $i<$n; $i++){
        $myVal = next();
    }
    echo $myVal;
    

    There are other ways to access a specific element, already mentioned by @deceze.

提交回复
热议问题